C# static keyword

In this tutorial, we would learn about static keyword in c# programming language.
static : something which belongs to the type itself, not with an instance of the type.
The modifier in C# language applies to the following :
Variable
Methods
Class
Constructor
Now we would look at examples of each
static variable
static variables are accessed with the name of the class.
Only a single copy of the variable is created and shared among all the instances of the class.
static variable can't be accessed with the instance.
varcan not be used to declare a static variable, one must specify the type of variable after the static keyword.
public class Employee{
public static int EmpCount = 0;
public void AddEmployee(){
EmpCount++;
}
}
public static void Main(string[] args){
Console.WriteLine("Start Of the Program");
Employee emp1 = new Employee();
emp1.AddEmployee();
Console.WriteLine(Employee.EmpCount); // output 1
Employee emp2 = new Employee();
emp2.AddEmployee();
Console.WriteLine(Employee.EmpCount); // output 2
Console.WriteLine(emp2.EmpCount); // compilation error
Console.ReadKey();
}
static method
Similar to static variables, static methods are also accessed with the name of the class.
static methods could use both static and non-static class members.
static members are directly accessed inside the static method without a class name, and non-static members are accessed using instances.
public class Employee {
private string FirstName;
private string LastName;
public Employee(string firstName, string lastName) {
FirstName = firstName;
LastName = lastName;
}
public string FullName {
get{
return ($"{this.FirstName} {this.LastName}");
}
}
public static int EmpCount = 0;
public static void AddEmployee(Employee emp) {
EmpCount++;
Console.WriteLine(emp.FullName);
}
}
public static void Main(string[] args) {
Console.WriteLine("Start Of the Program");
Employee emp1 = new Employee("John", "Smith");
Employee.AddEmployee(emp1); // "John Smith" : instance specific data
Console.WriteLine(Employee.EmpCount); // 1: static data
Employee emp2 = new Employee("John", "Bold"); // "John Bold" : instance specific data
Employee.AddEmployee(emp2);
Console.WriteLine(Employee.EmpCount); // 2: static data
Console.ReadKey();
}
static constructor
a static constructor is used to initialize static data or to perform an action that needs to be performed only once.
A class can have only one static constructor.
No parameter, No access modifier, No inheritance or overloading
static methods could use both static and non-static class members.
a static constructor is called automatically by the CLR. The user has no control over when a static constructor is executed in the program. static constructor runs before the instance constructor.
a static constructor is called only for a single time during program execution.
public class Organization {
static Organization() {
Console.WriteLine("Static constrctor called");
}
public Organization() {
Console.WriteLine("Instance constructor called");
}
}
public static void Main(string[] args) {
Console.WriteLine("Start Of the Program");
Organization org1 = new Organization();
Console.ReadKey();
}
** Output **
Start Of the Program
Static constrctor called
Instance constructor called
static class
static class can only contain static data members, static methods and static constructor.
[Error if there are non-static fields, the non-static method in static class]Instances can not be created for a static class.
[Error if user tries to create an instance of static class]static classes are sealed, they can't be inherited.
static members can be accessed directly without using the class name inside the static class.
public static class Organization {
private static string FounderName = "James Clark";
private static DateTime StartDate = new DateTime(1990, 01, 01);
public static string Introduction;
static Organization() {
Introduction = $"This organization was founded by {FounderName} in {StartDate.Year}";
}
}
public static void Main(string[] args) {
Console.WriteLine("Start Of the Program");
Console.WriteLine(Organization.Introduction);
}
** Output **
Start Of the Program
This organization was founded by James Clark in 1990.
In this post, we have learned about static keyword in c#. Please share your feedback and suggestios to improve this.


