0

C# supports the static modifier on a class definition to define a class which cannot be inherited, is sealed, and has only static members. VB.NET supports Module statement to define a reference type available through its namespace, to similar effect.

I know that C# and VB.NET share a common CodeDOM, Roslyn compiler, and target IL. My question is what, if any, difference there is between a static class vs. module.

1
  • 1
    You can easily out for yourself using any IL inspection tool (like ILSpy). A VB.NET Module is nothing more or less than a sealed class with static members marked with the StandardModule attribute. The only remarkable thing is that the class itself is not marked static (abstract sealed on the IL level), though this makes little difference in practice of course. Commented Feb 19, 2021 at 19:51

1 Answer 1

0

There are only two real differences as far as I am concerned in that a module cannot be inherited nor does it have a constructor.

Aside from that a module is extremely similar to a non-inheritable class where the class only contains shared (vb.net equivalent to static) members.

For example, the following module:

Public Module Test

    Public Function Foo() As String
        Return "Foo"
    End Function

End Module

Can be similarly expressed as:

Public NotInheritable Class Test

    Public Shared Function Foo() As String
        Return "Foo"
    End Function

End Class

C# developers will likely be more comfortable with the latter whereas legacy Visual Basic developers would go for the former.

Sign up to request clarification or add additional context in comments.

6 Comments

There are only two real differences as far as I am concerned in that a module ... nor does it have a constructor - not sure I completely agree that this is a "difference to C#" - both languages have the notion of static constructors/methods that are run automatically in a static context for purposes of initialization; to whit, you can certainly put a Sub New() Throw New Exception() in your Module Module1 and the app will crash upon startup
@CaiusJard - That is because the starting point of the application is Module1's New method. A starting point for the application and a constructor are not the same thing.
Hmm no, it is because the static constructor will be called upon first reference. The Module1.New is a private, parameterless constructor.
I think you can prevent the class from being instantiated provide a Private constructor otherwise the default constructor is available.
Yes, I shouldn't have said "on startup" as it's slightly misleading/oversimplifying; it's true for when you have Sub Main and Sub New in Module1. I should have said "on first reference to anything in module1, which would be on startup if Main is in module1".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.