Curriculum
Static Members and Static Classes in C# are important concepts used to create data and functionality that belong to a class rather than individual objects. Static Members and Static Classes in C# help developers share data, improve performance, reduce memory usage, and build utility functionality that can be accessed globally throughout an application. Every professional .NET developer uses Static Members and Static Classes in C# while developing ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, Cloud Solutions, and Enterprise Software Systems.
Understanding Static Members and Static Classes in C# is essential because they are widely used in utility classes, configuration management, logging systems, helper methods, and application-wide settings.
Static Members in C# belong to the class itself rather than to objects created from the class.
Normally:
Student student1 =
new Student();
Student student2 =
new Student();
Each object has its own copy of non-static members.
However:
public static int Count;
There is only one copy shared by all objects.
Static members are stored in memory once and shared across the entire application.
Static Members and Static Classes in C# help developers:
These benefits make static members useful in enterprise applications.
| Static Member | Non-Static Member |
|---|---|
| Belongs to Class | Belongs to Object |
| One Copy Exists | Multiple Copies Exist |
| Accessed Using Class Name | Accessed Using Object |
| No Object Required | Object Required |
| Shared by All Objects | Separate for Each Object |
Understanding this distinction is important for software design.
A Static Field is shared by all objects of a class.
Example:
public class Student
{
public static int Count;
}
Usage:
Student.Count = 10;
Console.WriteLine(
Student.Count);
Output:
10
No object is required to access the static field.
Example:
public class Employee
{
public static string
CompanyName =
"Groot Software";
}
Usage:
Console.WriteLine(
Employee.CompanyName);
Output:
Groot Software
All employees share the same company name.
A Static Method belongs to the class rather than an object.
Example:
public class Calculator
{
public static int Add(
int a,
int b)
{
return a + b;
}
}
Usage:
int result =
Calculator.Add(10,20);
Console.WriteLine(result);
Output:
30
Static methods are commonly used for utility operations.
Static Methods are useful when:
Examples:
Math Calculations
String Utilities
Validation Functions
Configuration Helpers
These methods simplify application development.
Example:
public class Utility
{
public static void Display()
{
Console.WriteLine(
"Welcome to C#");
}
}
Usage:
Utility.Display();
Output:
Welcome to C#
No object creation is required.
A Static Constructor initializes static members.
Example:
public class Student
{
static Student()
{
Console.WriteLine(
"Static Constructor Executed");
}
}
Characteristics:
Static constructors are useful for application initialization.
Example:
public class Company
{
public static string Name;
static Company()
{
Name =
"Groot Software";
}
}
Usage:
Console.WriteLine(
Company.Name);
Output:
Groot Software
The static constructor initializes the value automatically.
Properties can also be static.
Example:
public class Settings
{
public static string
ApplicationName
{
get;
set;
}
}
Usage:
Settings.ApplicationName =
"Student Portal";
Console.WriteLine(
Settings.ApplicationName);
Output:
Student Portal
Static properties are useful for global settings.
A Static Class contains only static members.
Example:
public static class Calculator
{
public static int Add(
int a,
int b)
{
return a + b;
}
}
Usage:
int result =
Calculator.Add(5,10);
Output:
15
Static Classes cannot be instantiated.
Static Classes:
These restrictions ensure proper use.
Example:
Calculator calculator =
new Calculator();
Output:
Compilation Error
Objects cannot be created from static classes.
The .NET Framework provides:
Math.Sqrt(25);
Math.Pow(2,3);
Output:
5
8
The Math class is a common example of a static class.
Example:
public static class AppSettings
{
public static string
ConnectionString
{
get;
set;
}
}
Usage:
AppSettings.ConnectionString =
"Database Connection";
All application components can access the same setting.
Example:
public class Student
{
public static int Count;
public Student()
{
Count++;
}
}
Usage:
Student s1 =
new Student();
Student s2 =
new Student();
Console.WriteLine(
Student.Count);
Output:
2
Static fields are often used for tracking statistics.
| Static Method | Instance Method |
|---|---|
| Belongs to Class | Belongs to Object |
| No Object Required | Object Required |
| Access Static Members Directly | Access Instance Members Directly |
| Faster for Utility Operations | Used for Object Behavior |
Both serve different purposes.
Example:
public class Student
{
public static string
College =
"Groot Academy";
public string Name;
}
Usage:
Student student =
new Student();
student.Name = "Rahul";
Console.WriteLine(
Student.College);
Console.WriteLine(
student.Name);
Output:
Groot Academy
Rahul
This demonstrates the difference clearly.
No object creation required.
Only one copy exists.
Common information is centrally managed.
Members can be accessed globally.
Ideal for helper methods.
These benefits make static functionality valuable in large applications.
Interest Rate
Bank Name
Configuration Settings
Tax Percentage
Application Settings
Global Discounts
Hospital Name
System Configuration
Global Policies
School Name
Academic Year
Configuration Settings
Static members help manage shared information efficiently.
Not everything should be static.
Static members are shared globally.
Static data may be accessed simultaneously.
Separate responsibilities clearly.
Static Members and Static Classes in C# are heavily used in:
A strong understanding of Static Members and Static Classes in C# helps developers create efficient, maintainable, and scalable software solutions.
Static Members belong to the class rather than individual objects.
A Static Field stores data shared by all objects of a class.
A Static Method belongs to a class and can be called without creating an object.
A Static Constructor initializes static members and executes only once.
A Static Class contains only static members and cannot be instantiated.
They help manage shared data, utility functionality, application settings, and improve performance.
WhatsApp us