“Static” usage in C# : it’s not only a keyword
The static
keyword in C# is a versatile modifier that can be applied to various elements within the language. Depending on where it's used, static
behaves differently and serves different purposes.
In this blog, we'll unravel the mysteries of the static
keyword, exploring its behaviours in different contexts and providing code snippets for clarity.
Embark on a journey of continuous learning and exploration with DotNet-FullStack-Dev. https://dotnet-fullstack-dev.blogspot.com/
Static Members in a Class
Behaviour
When applied to class members (fields, methods, properties), the static
keyword indicates that the member belongs to the class itself rather than an instance of the class. It is shared among all instances of the class.
public class MathOperations
{
public static int Add(int a, int b)
{
return a + b;
}
public static double SquareRoot(double value)
{
return Math.Sqrt(value);
}
public static int Counter { get; set; } = 0;
}
In this example, Add
and SquareRoot
are static methods, and Counter
is a static property. They can be accessed using the class name, not requiring an instance of MathOperations
.
Static Class
Behaviour
A static
class is a class that cannot be instantiated, and all its members must be static. It is commonly used for grouping related functionality without the need for instances.
public static class Logger
{
public static void LogMessage(string message)
{
Console.WriteLine($"[LOG] {message}");
}
}
The Logger
class is static, and its LogMessage
method is also static. Access it directly without creating an instance of Logger
.
Static Constructors
Behaviour
A static constructor in a class is called only once when the class is first accessed, and it’s used to initialize static members.
public class Singleton
{
private static readonly Singleton instance;
static Singleton()
{
instance = new Singleton();
}
private Singleton() { }
public static Singleton Instance
{
get { return instance; }
}
}
The static
constructor ensures that the Singleton
class is initialized only once when accessed.
Static Methods in a Non-Static Class
Behaviour
In a non-static class, static
methods can be used for utility functions that don't rely on instance-specific data.
public class Utility
{
public static int CalculateSquare(int x)
{
return x * x;
}
}
Here, CalculateSquare
is a static method in a non-static class, providing utility functionality.
Conclusion
The static
keyword in C# is a powerful tool with various use cases. Whether applied to class members, classes themselves, constructors, or methods within a non-static class, static
provides a means of organizing and sharing functionality without the need for object instances.
As you navigate the C# landscape, understanding the different behaviors of the static
keyword empowers you to design more efficient, modular, and expressive code. Whether you're creating utility classes, implementing singletons, or simply organizing related functionality, the static
keyword is a valuable asset in your programming toolkit.