static C# with Example



static C# with Example

The 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 to the total number of instances of A class. 
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 are called before the class is referenced for the first time. 
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 function, 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 method belonging to static class Console 
} 
} 
Example with using static 
using static System.Console; 
public class ConsoleApplication 
{ 
public static void Main() 
{ 
WriteLine("Hello World!"); //Writeline is method belonging to 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, the class is loaded into memory and cannot be run through the 
garbage collector until the AppDomain housing the static class is unloaded. 
A static class cannot implement an interface. 

0 Comment's

Comment Form

Submit Comment