What is the equivalent of a VB module in C#?

In VB.NET, a module is used to store loose code and variables that are accessible from anywhere in the application without needing to instantiate an object. Variables in a module maintain their state throughout the application lifetime.

The C# equivalent of a VB.NET module is a static class. Static classes provide the same functionality − global accessibility without instantiation and persistent state through static members.

Syntax

Following is the syntax for creating a static class in C# −

public static class ClassName {
    public static void MethodName() {
        // method implementation
    }
    
    public static int VariableName = value;
}

VB.NET Module Example

Here's how a module looks in VB.NET −

Module MyModule
    Public counter As Integer = 0
    
    Public Sub Display()
        MsgBox("Demo!")
    End Sub
    
    Public Sub IncrementCounter()
        counter = counter + 1
    End Sub
End Module

C# Static Class Equivalent

The equivalent static class in C# provides the same functionality −

using System;

public static class MyModule {
    public static int counter = 0;
    
    public static void Display() {
        Console.WriteLine("Demo!");
    }
    
    public static void IncrementCounter() {
        counter++;
    }
}

public class Program {
    public static void Main() {
        MyModule.Display();
        
        Console.WriteLine("Initial counter: " + MyModule.counter);
        MyModule.IncrementCounter();
        MyModule.IncrementCounter();
        Console.WriteLine("After incrementing: " + MyModule.counter);
    }
}

The output of the above code is −

Demo!
Initial counter: 0
After incrementing: 2

Key Characteristics

VB.NET Module C# Static Class
Members are accessible without module name Members accessed with class name prefix
Cannot be instantiated Cannot be instantiated
All members are implicitly shared/static All members must be explicitly static
Variables maintain state globally Static variables maintain state globally

Practical Example with Multiple Methods

using System;

public static class MathUtilities {
    public static double Pi = 3.14159;
    
    public static double CalculateCircleArea(double radius) {
        return Pi * radius * radius;
    }
    
    public static double CalculateCircleCircumference(double radius) {
        return 2 * Pi * radius;
    }
}

public class Program {
    public static void Main() {
        double radius = 5.0;
        
        Console.WriteLine("Radius: " + radius);
        Console.WriteLine("Area: " + MathUtilities.CalculateCircleArea(radius));
        Console.WriteLine("Circumference: " + MathUtilities.CalculateCircleCircumference(radius));
    }
}

The output of the above code is −

Radius: 5
Area: 78.5395
Circumference: 31.4159

Conclusion

C# static classes serve as the direct equivalent to VB.NET modules, providing global accessibility and persistent state without instantiation. The main difference is that C# requires explicit static keywords and class name prefixes when accessing members.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements