VB.NET has classes and Modules, so my first question is what is the difference? Also, I noticed that C# does not have modules, but just classes, is there something in place of modules or were they removed for C#?
Asked
Active
Viewed 1.6k times
1 Answers
33
About the closest thing to a VB module would be a static class in C#.
For Example:
In VB.NET
Module SomeModule
Public Sub DoSomething
MsgBox("Doing something!")
End Sub
End Module
Same thing in C#:
public static class DoSomethingFuncs
{
public static void DoSomething() {
MessageBox.Show("Doing something!");
}
}

squillman
- 13,363
- 3
- 41
- 60
-
1(For us unenlightened -- at least me -- it would be interesting to have a little section on comparing VB modules vs C# static classes.) – Mar 16 '11 at 21:02
-
5Yes, a Module in VB.Net equals to a static class in C#, it also has the same limitations (such as it can't inherit, there's always only one instance, all members are shared). – Aidiakapi Mar 16 '11 at 21:03
-
6@Aidiakapi Jared says in a [comment on this answer](http://stackoverflow.com/questions/881570/classes-vs-modules-in-vb-net/881586#881586) "Modules are not exactly the same as static classes in C#. Methods in a module are effectively global if they are in an imported namespace." For example in the VB code above you can write `DoSomething` in any class, but in the C# it has to be qualified with the class name `DoSomethingFuncs.DoSomething` – MarkJ Mar 17 '11 at 12:34
-
@MarkJ, that's the way they're called, not the way they work. Their scope is indeed wider then the one of a static class. One remarkable thing is that decompiling a VB.Net Module to C# results in: `[StandardModule] internal sealed class Module1` while decompiling a static class (with the name Module1) in C# results in: `internal abstract sealed class Module1`. So the difference is the class argument (probably there for reflection), and that a static class in C# is also flagged as abstract. – Aidiakapi Mar 17 '11 at 13:07