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 vs. Non-Static method in C#
In C#, methods can be declared as either static or non-static (instance methods). Static methods belong to the class itself and can be called without creating an instance of the class, while non-static methods belong to specific instances of the class.
Static methods can only access static variables and other static members directly. They exist even before any object of the class is created and are shared across all instances of the class.
Syntax
Following is the syntax for declaring a static method −
public static returnType MethodName() {
// method body
}
Following is the syntax for declaring a non-static (instance) method −
public returnType MethodName() {
// method body
}
Key Differences
| Static Methods | Non-Static Methods |
|---|---|
Called using the class name: ClassName.Method()
|
Called using object instance: objectName.Method()
|
| Can only access static members directly | Can access both static and non-static members |
No this keyword available |
Can use this keyword to refer to current instance |
| Exist before object creation | Exist only when object is created |
Using Static Methods
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) {
// Calling static methods without creating object
int sum = Calculator.Add(10, 5);
int product = Calculator.Multiply(4, 3);
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Product: " + product);
}
}
The output of the above code is −
Sum: 15 Product: 12
Using Non-Static Methods
Example
using System;
class Counter {
private int count = 0;
public void Increment() {
count++;
}
public int GetCount() {
return count;
}
}
class Program {
static void Main(string[] args) {
// Creating objects to use non-static methods
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.Increment();
c1.Increment();
c2.Increment();
Console.WriteLine("Counter 1: " + c1.GetCount());
Console.WriteLine("Counter 2: " + c2.GetCount());
}
}
The output of the above code is −
Counter 1: 2 Counter 2: 1
Combining Static and Non-Static Methods
Example
using System;
class StaticVar {
public static int num = 0;
public void Count() {
num++;
}
public static int GetNum() {
return num;
}
}
class Program {
static void Main(string[] args) {
StaticVar s1 = new StaticVar();
StaticVar s2 = new StaticVar();
s1.Count();
s2.Count();
s1.Count();
Console.WriteLine("Static variable num: " + StaticVar.GetNum());
}
}
The output of the above code is −
Static variable num: 3
Common Use Cases
Static methods are ideal for utility functions, mathematical operations, and methods that don't depend on instance data. Examples include Math.Max(), Console.WriteLine(), and validation methods.
Non-static methods are used when you need to work with instance-specific data, maintain object state, or when the behavior depends on the particular object instance.
Conclusion
Static methods belong to the class and can be called without object instantiation, while non-static methods belong to specific object instances. Choose static methods for utility functions and non-static methods when working with instance-specific data and behavior.
