Static Class :

May 11, 2011

A static class is you can say same as the non-static class, but there is one difference them is a static class can't be instantiated. In other words, you cannot use the "new" keyword to create a instance variable of the class type. As there is no instance variable, you can access the members of a static class by using the name of the class itself. Static classes and it's class members are used to create data & functions which can be accessed without creating an instance of that class. Static class members can be used to separate data and behavior that is independent of any object identity, the data & functions do not change regardless of what happens to the object. Static classes can be used when data or behavior is not present in the class that depends on object identity. Static classes are automatically loaded by the .NET Framework CLR when that program or namespace containing the class is loaded.

The features of a static class are as follows:

  • static classes can only contain static members.
  • static classes can not be instantiated.
  • static classes are sealed and therefore cannot be inherited.
  • static classes can not contain Instance Constructors (C# Programming Guide).
public static class Settings { static int i; public static string GetName() { return "MyName"; } } class Program { static void Main(string[] args) { string str=Settings.GetName(); Console.Write(str); Console.Read(); } }

Static Fields:

Static fields can be declared by using the keyword static. class MySettings { public static int height; public static int width = 20; }

When we declare a static field inside a class, it can be initialized as shown above in the example with a value. All un-initialized static fields get automatically initialized to their default values when the containing class is loaded for the first time.

For example

class MySettings { public static int height = 20; public static int width; public static int length = 25; public MySettings(int i) { height = i; width = i; length = i; } } class AllSettings { public static void Main() { Console.WriteLine("{0},{1},{2}", MySettings.height, MySettings.width, MySettings.length); MySettings mc = new MySettings(25); Console.WriteLine("{0},{1},{2}", MySettings.height, MySettings.width, MySettings.length); } }

Static Method:

Static methods can be declared using Static keyword befor method name. The static methods can by accessed directly from the class. Static methods are normally faster to invoke on the call stack than instance methods

class MySettings { private static int height = 100; private static int width = 150; public static void MyMethod() { Console.WriteLine("{0},{1}", height, width); } } class AllSettings { public static void Main() { MyClass.MyMethod(); } }

Abstract Class :

An abstract class is the one that is not used to create objects. An abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built. Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on it's own, it must be inherited. Like interfaces, abstract classes can specify members that must be implemented in inheriting classes. Unlike interfaces, a class can inherit only one abstract class. Abstract classes can only specify members that should be implemented by all inheriting classes.

Important :
  • An abstract class cannot be a sealed class or static.
  • Declarations of abstract methods are only allowed in abstract classes.
  • An abstract method cannot be private.
  • The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
public abstract class ABstractClass { static int i; public int GetValue(int first,int second) { return first + second; } } class Program : ABstractClass { static void Main(string[] args) { // Program sd = new Program (); // This will give error as you can not create instance of Static class Program cc = new Program(); int value = cc.GetValue(10, 20); Console.Write("Total : " + value.ToString()); Console.Read(); } }

Abstract method:

An abstract method is a method without any method body. Because an abstract method provides does not have actual implementation and the method body of an abstract method simply consists of a semicolon (;). When a class inherits from an abstract class, the derived class must implement all the abstract methods declared in the base class. But by declaring the derived class also abstract, we can avoid the implementation of all or certain abstract methods. This is what is known as partial implementation of an abstract class

public abstract class Employee { public abstract void CalculateSalry(int DailyAllowance, int Presentdays); } public class TemporaryEmployee : Employee { public override void CalculateSalry(int DailyAllowance, int Presentdays) { //Calculate Salary for Temporary Employee } } public class PermanenetEmployee : Employee { public override void CalculateSalry(int DailyAllowance, int Presentdays) { //Calculate Salary for Permanenet Employee } }