Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the differences between a static and a non-static class in C#?
A static class in C# cannot be instantiated and contains only static members, while a non-static class can be instantiated to create objects and can contain both static and instance members.
The key difference is that static classes are designed for utility functions that don't require object state, whereas non-static classes represent entities that can have multiple instances with their own data.
Syntax
Following is the syntax for declaring a static class −
public static class ClassName {
public static void StaticMethod() {
// static method implementation
}
}
Following is the syntax for declaring a non-static class −
public class ClassName {
public void InstanceMethod() {
// instance method implementation
}
public static void StaticMethod() {
// static method implementation
}
}
Key Differences
| Static Class | Non-Static Class |
|---|---|
Cannot be instantiated using new keyword |
Can be instantiated using new keyword |
| Can only contain static members | Can contain both static and instance members |
| Members accessed using class name | Instance members accessed via object, static members via class name |
| Implicitly sealed (cannot be inherited) | Can be inherited unless marked sealed |
| Cannot have instance constructors | Can have instance constructors |
Using Static Classes
Example
using System;
public static class MathHelper {
public static int Add(int a, int b) {
return a + b;
}
public static int Multiply(int a, int b) {
return a * b;
}
public static double GetCircleArea(double radius) {
return Math.PI * radius * radius;
}
}
public class Program {
public static void Main() {
// Access static class members directly using class name
int sum = MathHelper.Add(10, 20);
int product = MathHelper.Multiply(5, 6);
double area = MathHelper.GetCircleArea(3.0);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
Console.WriteLine("Circle Area: " + area);
// This would cause compile error:
// MathHelper helper = new MathHelper(); // Cannot instantiate static class
}
}
The output of the above code is −
Sum: 30 Product: 30 Circle Area: 28.2743338823081
Using Non-Static Classes
Example
using System;
public class Calculator {
private string name;
public Calculator(string name) {
this.name = name;
}
// Instance method
public int Add(int a, int b) {
Console.WriteLine(name + " is adding numbers");
return a + b;
}
// Static method
public static int Multiply(int a, int b) {
return a * b;
}
}
public class Program {
public static void Main() {
// Create instances of non-static class
Calculator calc1 = new Calculator("Calculator-1");
Calculator calc2 = new Calculator("Calculator-2");
// Call instance methods
int sum1 = calc1.Add(10, 5);
int sum2 = calc2.Add(20, 8);
// Call static method using class name
int product = Calculator.Multiply(4, 7);
Console.WriteLine("Sum1: " + sum1);
Console.WriteLine("Sum2: " + sum2);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Calculator-1 is adding numbers Calculator-2 is adding numbers Sum1: 15 Sum2: 28 Product: 28
Common Use Cases
Static classes are ideal for utility functions, helper methods, and extension methods that don't require object state. Examples include Math, Console, and Convert classes in .NET.
Non-static classes are used for modeling real-world entities that can have multiple instances with different states, such as Person, Car, or BankAccount objects.
Conclusion
Static classes cannot be instantiated and contain only static members accessed via the class name, while non-static classes can be instantiated into multiple objects with their own state. Choose static classes for utility functions and non-static classes for entities that need multiple instances.
