one is no object of static class can be created
another is, a static class must contain only static members,
then it is important that what is the main benefit to create a static class, the main benefit of making static class,
we do not need to make any instance of this class ,all members can be accessible with its own name.
Declaration:
A static class is created by using keyword 'Static' as shown here:
Static class Clasname
{
//C#
}
One more thing that is notable-within static class, all members must be explicitly specified as static,
static class does not automatically make its members static. Static class can contain a collection of static methods.
Example:
using System;
static class Shape
{
public static double GetArea(double Width, double height)
{
return Width * Height;
}
}
class Ractangle
{
private void GetRactangleArea()
{
Double Area;
Area = Shape.GetArea(10, 5);
}
}
Shape is static class, it contain staic function GetArea.Ractangle is other class and with in GetArea function can be access without creating instace of Class Shape.
Although a static class cannot have an instance constructor, it can have a static constructor.
Static Variable Memory :
Static variables are stored on the Managed Heap, not the Stack, when the type is first referenced. The Type Object of the compiled class contains a reference to the object.
The Type Object of a class will stay in memory until the AppDomain where it resides is unloaded. Since the object on the Heap is always being referenced by the compiled Type Object, static objects on the Heap will never by GC'ed and will always consume memory until the AppDomain is unloaded
No comments:
Post a Comment