C# static keyword

Search for a command to run...

No comments yet. Be the first to comment.
C# types can be divided mainly into two categories: value types and reference types. In this article, we would be having a closer look at these two types. Value Types Most of the c# built-in types such as numeric types, char types and boolean types ...
ASP.NET Core Configuration Guide Overview ASP.NET Core uses a layered configuration system where later sources override earlier ones for the same key. Configuration Priority Order Priority Source
Legacy Delphi applications often struggle with outdated UI/UX, performance issues, and monolithic architectures that make refactoring difficult. However, a full rewrite is not always feasible due to time, cost, and the risk of disrupting critical bus...
Entity Framework Core (EF Core) is a powerful and flexible object-relational mapping (ORM) framework for .NET applications, enabling developers to work with relational databases using strongly-typed .NET objects. It simplifies data access by abstract...

In this article we are going to cover some advance scenarios where Options pattern is very useful. We have covered the basic understanding of Options pattern into Options pattern in ASP.NET Core: Introduction article. Please go through it if not fami...

In this article we will learn about Options pattern in ASP.NET Core for dealing with application configurations. The Options pattern uses the C# classes to access and manage the configurations thus providing more flexibility and type safe approach to...

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 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.
var can 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();
}
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();
}
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 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.