Overview of Static Members in C#
Posted by
Manas
at
12:59:00 AM
Reactions:
Static Class:
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
The following list provides the main features of a static class:
- Contains only static members.
- Cannot be instantiated.
- Is sealed.
- Cannot contain Instance Constructors.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.
Static Fields:
When a field declaration includes a static modifier, the fields introduced by the declaration are static fields.
Static fields have the following properties:
- A static field is not part of a specific instance instead; it identifies exactly one storage location.
- A static field is always accessed by the class name, not the instance name.
- If your class contains static fields, provide a static constructor that initializes them when the class is loaded.
C# does not support static local variables (variables that are declared in method scope).
Static fields can be declared as follows by using the keyword static.
class MyClass
{
public static int x;
public static int y = 20;
}
When we declare a static field inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded first time.
Static Constructor:
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.
Static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- The name of a static constructor must be the name of the class and even they don't have any return type.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
Example:
// C# static constructor
class MyClass
{
public static int x;
public static int y;
static MyClass()
{
x = 100;
y = 200;
}
}
class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1},{2}", MyClass.x, MyClass.y);
}
}
Static Functions:
A member functions can also be declared as static. But a static member function can access only other static members. They can access non-static members only through an instance of the class.
We can invoke a static member only through the name of the class.
Example:
// C# static Member function
class MyClass
{
private static int x = 20;
private static int y = 40;
public static void Method()
{
Console.WriteLine("{0},{1}", x, y);
}
}
class MyClient
{
public static void Main()
{
MyClass.Method();
}
}
Static Properties:
The properties also in C# can be declared as static. The static properties are accessing using the class name.
Example:
// C#: static Properties
class MyClass
{
public static int X
{
get
{
Console.Write("GET");
return 10;
}
set
{
Console.Write("SET");
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 20; // calls setter displays SET
int val = MyClass.X;// calls getter displays GET
}
}
Example:
// C#:static Override
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("Base static method");
}
}
class MyClass : MyBase
{
public new static int x = 50;
public new static void Method()
{
Console.WriteLine("Derived static method");
}
}
class MyClient
{
public static void Main()
{
MyClass.Method(); // Displays 'Derived static method'
Console.WriteLine(MyClass.x);// Displays 50
}
}
Hope It Helps...!
Labels: C# Basics
Subscribe to:
Post Comments (Atom)



1 comments:
very best explanation
Post a Comment