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
static keyword in C#
The static keyword in C# is used to declare class members that belong to the class itself rather than to any specific instance. When a member is declared as static, it means there is only one copy of that member shared across all instances of the class.
Static members can be accessed directly using the class name without creating an object instance. They are commonly used for utility methods, constants, and shared data that should be consistent across all instances of a class.
Syntax
Following is the syntax for declaring static members −
public static dataType variableName;
public static returnType MethodName() { }
public static class ClassName { }
Static members are accessed using the class name −
ClassName.staticVariable = value; ClassName.StaticMethod();
Static Variables
Static variables maintain their value across all instances of a class and are initialized only once when the class is first loaded −
Example
using System;
class StaticVar {
public static int num;
public void count() {
num++;
}
public int getNum() {
return num;
}
}
class StaticTester {
static void Main(string[] args) {
StaticVar s1 = new StaticVar();
StaticVar s2 = new StaticVar();
s1.count();
s1.count();
s1.count();
s2.count();
s2.count();
s2.count();
Console.WriteLine("Variable num for s1: {0}", s1.getNum());
Console.WriteLine("Variable num for s2: {0}", s2.getNum());
}
}
The output of the above code is −
Variable num for s1: 6 Variable num for s2: 6
Static Methods
Static methods belong to the class and can be called without creating an instance. They cannot access non-static members directly −
Example
using System;
class Calculator {
public static int Add(int a, int b) {
return a + b;
}
public static int Multiply(int a, int b) {
return a * b;
}
}
class Program {
static void Main(string[] args) {
int sum = Calculator.Add(10, 20);
int product = Calculator.Multiply(5, 6);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Sum: 30 Product: 30
Static Classes
A static class cannot be instantiated and can only contain static members. It is commonly used for utility classes −
Example
using System;
static class MathUtils {
public static double Pi = 3.14159;
public static double CircleArea(double radius) {
return Pi * radius * radius;
}
public static double CirclePerimeter(double radius) {
return 2 * Pi * radius;
}
}
class Program {
static void Main(string[] args) {
double radius = 5.0;
double area = MathUtils.CircleArea(radius);
double perimeter = MathUtils.CirclePerimeter(radius);
Console.WriteLine("Circle with radius {0}:", radius);
Console.WriteLine("Area: {0:F2}", area);
Console.WriteLine("Perimeter: {0:F2}", perimeter);
}
}
The output of the above code is −
Circle with radius 5: Area: 78.54 Perimeter: 31.42
Key Rules
-
Static members are shared across all instances of a class.
-
Static members are accessed using the class name, not object instances.
-
Static methods cannot access non-static members directly.
-
Static classes cannot be instantiated and can only contain static members.
-
Static variables are initialized only once when the class is first loaded.
Conclusion
The static keyword in C# creates class-level members that are shared across all instances. Static members are accessed using the class name and are commonly used for utility methods, constants, and shared data that remains consistent throughout the application lifecycle.
