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. For example, if you have a static class that is named UtilityClass that has a public method named MethodA, you call the method as shown in the following example:
UtilityClass.MethodA();
The following list provides the main features of a static class:
Contains only static members.
Cannot be instantiated.
Is sealed. ( therefore static class cannot be inherited )
Cannot contain Instance Constructors. ( however, they can contain a static constructor.)
a static class remains in memory for the lifetime of the application domain in which your program resides.
When to use Static Class: A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields.
For example, in the .NET Framework Class Library, the static System.Math class contains several methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class.
Note: Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.
Advantage of Static Class:
public static class MyCalc
{
static MyCalc()
{
}
MyCalc obj = new MyCalc(); //compiler wont allow you to create this...it will show error...
}
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Here is an example of a static class that contains two methods that convert temperature from Celsius to Fahrenheit and from Fahrenheit to Celsius:
public static class TemperatureConverter {---------------------------------------------------------------------------------------------------
public static double CelsiusToFahrenheit(string temperatureCelsius)
{ // Convert argument to double for calculations.
double celsius = Double.Parse(temperatureCelsius);
// Convert Celsius to Fahrenheit.
double fahrenheit = (celsius * 9 / 5) + 32;
return fahrenheit;
}
public static double FahrenheitToCelsius(string temperatureFahrenheit)
{ // Convert argument to double for calculations.
double fahrenheit = Double.Parse(temperatureFahrenheit);
// Convert Fahrenheit to Celsius.
double celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
Calling static methods...
TemperatureConverter.CelsiusToFahrenheit(12);
TemperatureConverter.FahrenheitToCelsius(13);
Static/Shared members :
Static / Shared variables declared in a class are shared among all the objects that are created for a Class.
For every new object is created for a Class, all the members of the Class are allocated memory. But Static / Shared members of the Class are allocated memory only once irrespective of number of objects created.
Memory for Static / Shared member is allocated when Class is loaded.
A Class is loaded when first instance(Object) is created.
These members are also called as Class members and are accessed outside the class using the Class name.
public class MyClass
{
public static int a;
}
MyClass.a = 10;
Two common uses of static fields are
- to keep a count of the number of objects that have been instantiated, or
- to store a value that must be shared among all instances.
A const field is essentially static in its behavior.
Note : C# does not support static local variables (variables that are declared in method scope)
If your class contains static fields, provide a static constructor that initializes them when the class is loaded.
---------------------------------------------------------------------------------------------------
Static/Shared Constructor :
1. Static/Shared Constructor is executed by CLR when Class is loaded. So this is used to initialize Static/Shared members dynamically.
2. Static Constructor cannot be overloaded ( Normal constructor can be overloaded )
3. Static Constructor has no access specifiers like private/public.
Series of events that occur when the first object is created for a Class .....
* Class is loaded
* Static members are loaded and allocated memory.
* Static Constructor is called.
* Normal members are loaded and allocated memory.
* Normal Constructor is called.
Observe the above series.....Point to be noted is you cannot access normal members of a class in Static Constructor as they are loaded after Static Constructor is executed.
A Class in C# can be declared as Static. Such a Class should has static members only. This is not supported in VB.NET.
-------------------------------------------------------------------------------------------------
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.