Friday 16 May 2014

Static Class C#

A static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type.
·         Contains only static members.
·         Is sealed.
·         A non-static class can contain static methods, fields, properties, or events.
·         Always accessed by the class name
·         It is more typical to declare a non-static class with some static members, than to declare an entire class as static.
·         Static methods and properties cannot access non-static fields and events
·         Static fields store a value that must be shared among all instances.
·         Static methods can be overloaded but not overridden
·         C# does not support static local variables (variables that are declared in method scope).
·         Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called.
·          Static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods.
·         No instantiation, no disposal, just fire'n'forget.
·         An abstract class is usually intended to model something in a type hierarchy. For example, a truck is a kind of vehicle. A static class by contrast is not intended to model anything at all. It's just a convenient way of storing a bunch of code.
·         Static" on a class, what we actually do is generating an abstract sealed class with no public constructors. Since it is abstract, you cannot create one directly. Since it is sealed, you cannot create a more derived class and instantiate that.
·         Cannot contain Instance Constructors.( Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class.)
Instance constructors Eg
class Add
{
    public int x, y;

    // constructor 
    public Add()
    {
        x = 0;
        y = 0;
    }
}
EG Console.WriteLine(Math.Abs(dub));
Here Math is a static class

No comments:

Post a Comment