static
suggest changeThe static modifier is used to declare a static member, which does not need to be instantiated in order to be accessed, but instead is accessed simply through its name, i.e. DateTime.Now.
static can be used with classes, fields, methods, properties, operators, events, and constructors.
While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
class A
{
static public int count = 0;
public A()
{
count++;
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
A b = new A();
A c = new A();
Console.WriteLine(A.count); // 3
}
}
count equals the total number of instances of class A.
The static modifier can also be used to declare a static constructor for a class, to initialize static data or run code that only needs to be called once. Static constructors run before the first instance is created or any static member is referenced. typeof alone does not run them.
class A
{
static public DateTime InitializationTime;
// Static constructor
static A()
{
InitializationTime = DateTime.Now;
// Guaranteed to only run once
Console.WriteLine(InitializationTime.ToString());
}
}
A static class is marked with the static keyword, and can be used as a beneficial container for a set of methods that work on parameters, but don’t necessarily require being tied to an instance. Because of the static nature of the class, it cannot be instantiated, but it can contain a static constructor. Some features of a static class include:
- Can’t be inherited
- Can’t inherit from anything other than
Object - Can contain a static constructor but not an instance constructor
- Can only contain static members
- Is sealed
The compiler is also friendly and will let the developer know if any instance members exist within the class. An example would be a static class that converts between US and Canadian metrics:
static class ConversionHelper {
private static double oneGallonPerLitreRate = 0.264172;
public static double litreToGallonConversion(int litres) {
return litres * oneGallonPerLitreRate;
}
}
When classes are declared static:
public static class Functions
{
public static int Double(int value)
{
return value + value;
}
}
all functions, properties, or members within the class also need to be declared static. No instance of the class can be created. In essence a static class allows you to create bundles of functions that are grouped together logically.
Since C# 6, static can also be used alongside using to import static members and methods. They can be used then without class name.
Old way, without using static:
using System;
public class ConsoleApplication
{
public static void Main()
{
Console.WriteLine("Hello World!"); //WriteLine is a method belonging to the static class Console
}
}
Example with using static
using static System.Console;
public class ConsoleApplication
{
public static void Main()
{
WriteLine("Hello World!"); //WriteLine is a method belonging to the static class Console
}
}
Drawbacks
While static classes can be incredibly useful, they do come with their own caveats:
• Once the static class has been called, it is loaded into memory and its static state is not collected until the AppDomain that loaded it is unloaded.
• A static class cannot implement an interface.