Results 1 to 2 of 2

Thread: Questions Surrounding NotInheritable

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,415

    Questions Surrounding NotInheritable

    I've been developing in VB.NET for a while now (check the VBF join date), but I've only recently started using AI to help me develop. Something I do very frequently is give an LLM a database schema definition and tell it to give me the corresponding .NET POCO definition. It spits out the accurate definition probably 95% of the time in less than 30 seconds and greatly simplifies a tedious part of my job.

    When I was doing this in C#, I noticed that the generated code included the sealed modifier in the class, but when I'd do the same thing in VB.NET it omitted it. Doing some digging, I found that there is a corresponding MustInherit modifier, but I wanted to get some clarification rather than using it carte-blanche.

    I can understand using it for performance reasons, e.g. I have a class and nothing's inheriting it, then mark it as MustInherit and JIT can presumably do its thing to make the code more performant. Then if I ever need to inherit it, then I can remove the modifier from the parent class and add it to the child class.

    But I'm wondering about other use cases when the MustInherit modifier makes sense, and I was hoping for some input.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,278

    Re: Questions Surrounding NotInheritable

    Uh..."sealed" in C# is definitely not the equivalent of "MustInherit" in VB, it's actually more of an opposite

    "sealed" in C#:
    1) "sealed" in class-declaration: Prevents OTHER classes inheriting from it --> I'm a "sealed" Person --> Nope. No children from me. Period!
    2) "sealed" on a Function inside the class: Prevents derived classes from overriding it

    "MustInherit" in VB: Declares the Class to be abstract, and CANNOT be instantiated directly --> I'm a "MustInherit" Person --> Don't talk to me, ever! Talk to my children!

    A "MustInherit"-Class is more like a Template

    https://learn.microsoft.com/en-us/do...eywords/sealed
    https://learn.microsoft.com/en-us/do...rs/mustinherit

    You probably meant "NotInheritable" (as actually mentioned in your Thread-Subject)
    https://learn.microsoft.com/en-us/do...notinheritable

    Difference between C# ("sealed") and VB ("NotInheritable"): in VB it's only valid in the Class statement. You cannot seal single Functions like in C#
    otherwise if "sealed" (C#) is in the class-declaration, then it's equivalent to "NotInheritable" in VB

    though it escapes me how i compiler can gain performance by using it

    IMO, using "NotInheritable" in this context makes only sense if you are a developing a component, which is/will be used by OTHER programmers, and you want to protect your component from those programmers trying to change its behavior
    Last edited by Zvoni; Today at 04:02 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width