24-12-2010
Static Property: While defining a property if we use "static" keyword that property can be called as static property.
Eg: For Static Property:
namespace staticprpoerty
{
Public class myclass
{
Public static int x;
Public static int X
{
get
{
return x;
}
set
{
x=value;
}
}
Public static int y;
Public static int Y
{
get
{
return y;
}
set
{
y=value;
}
}
}
Class mainclass
{
void main()
{
mycalss.x = 20;
Console.WriteLine("X property value is " +myclass.x);
myclass mc = new myclass();
mc.Y = 30;
Console.WriteLine("Y property value is " +mc.Y);
}
}
}
Static Constructor:
While defining a constructor if we use static keyword that constructor can be called as static constructor.
Within the static constructor we can initialize only static variables we cannot initialize non static variables.
Static constructor cannot have the access modifier.
Static constructor cannot have the parameters i.e., it is a parameter unconstructor.
A class can contain maximum of only one static constructor.
Static constructor will execute when the class is loading.
Static variables we can initialize within the non-static constructor.
If a class is having a static constructor & a non static constructor first control will invoke static constructor then control will invoke non-static constructor because class is loading 1st then only object is creating.
A class have maximum only one statc constructor.
Eg: For static constructor & non static constructor:
namespace staticconstructor
{
Public class myclass
{
Public static int i;
Static myclass()
{
i = 10;
Console.WriteLine("Static constructor is calling");
}
Public int j;
Public myclass()
{
j = 20;
Console.WriteLine("Non-staic constructor is calling");
}
}
Class mainclass
{
Void main()
{
Console.WriteLine("i value is " +myclass.i);
myclass mc = new myclass();
Console.WriteLine("J value is " +mc.j);
Console.ReadLine();
}
}
}
Eg-1:
Public class myclass
{
Public static int i;
Public int j; --> non static variable
Static myclass()
{
i = 10;
j = 20;
}
}
The above code will generate an error because we cannot initialize a non static variable with in the static constructor.
Eg-2:
Public class myclass
{
Public static int i;
Public int j;
Pubic myclass()
{
i = 10; // non static constructor
j = 20;
}
Output: The above will not generate an error because within non static constructor we can initialize static variables.
Eg-3:
Public class myclass
{
Public static myclass
{
}
}
Output: Error, because static constructor cannot have access modifier.
Note: In C#.NET static indexers are not possible.
Memory Management:
In .NET technology memory management is handling by the garbage collector.
Garbage collector is an integral component of CLR. CLR is an integral component of .NET framework.
To perform the memory management garbage collector do two duties:
1. Allocating memory.
2. De-allocating memory or releasing memory.
When an object is created by the application garbage collector will allocate memory for that object within the managed heap.
Managed Heap:
Heap is a data structure which is using by the garbage collector to perform memory management due to that reason we will call it as managed heap.
When an object is not using by the application since long time garbage collector will recognize that object as unused object & it will destroy that unused object.
In .NET memory mangement can be calld as automatic memory management because programmer need not to write a single line of code.