Objective

This article will discuss about static class in c#. Different feature of static class and different way of creating static class also will be discussed. Finally one sample will give a hand on of the static class.

Static class is type of classes which cannot be instantiated. Instance of static class cannot be created.

10 Features of Static class

  1. A static class cannot be instantiated
  2. Static class can have only static members.
  3. Member of the Static class can be accessed by class name itself.
  4. Static class is sealed. So static class cannot be inherited.
  5. Static class contains only static constructors.
  6. Static class cannot have instance constructors.
  7. Static class can only be inherited only from object class.
  8. Static class is preceded by keyword static.
  9. Static constructor of static class called only once.
  10. Static class has private constructors.

How CLR deals with static class?

  1. Information of Static class is loaded by CLR, when the Program accessing or referencing Static class is being loaded.
  2. The program referencing static class has no control to specify when static class should be loaded by CLR
  3. Static class remains in memory for the lifetime of application domain in which program resides.

How to create Static class?

  1. Create a class with keyword Static.
  2. Create a private constructor inside class.
  3. Make all members as static.

Creating a Static class 

 

    1 using System;

    2 

    3 using System.Collections.Generic;

    4 

    5 using System.Linq;

    6 

    7 using System.Text;

    8 

    9 namespace Staticclass

   10 

   11 {

   12 

   13 public static class MyMath

   14 {

   15 

   16 public static int Add(int number1, int number2)

   17 {

   18 

   19 return number1 + number2;

   20 }

   21 

   22 public static int Sub(int number1, int number2)

   23 {

   24 

   25 return number1 – number2;

   26 }

   27 

   28 public static int Mul(int number1, int number2)

   29 {

   30 

   31 return number1 * number2;

   32 }

   33 public static int Div(int number1, int number2)

   34 {

   35 

   36 return number1 / number2;

   37 

   38 }

   39 }

   40 }

   41 

  1. Class is preceded with keyword Static
  2. Class is having 4 static methods.
  3. Each method is also preceded by keyword Static.

Using Static class in Program

On adding of reference of Static class, Intellisense will show all the static methods of static class. MyMath is name of the static class.

We do not need to create instance of the class , directly static methods could be called with class name.


 

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 

    6 namespace Staticclass

    7 {

    8 

    9 class Program

   10 {

   11 static void Main(string[] args)

   12 {

   13 

   14 Console.WriteLine(“Sumation= “+ MyMath.Add(7, 2).ToString());

   15 Console.WriteLine(“Substraction = “ + MyMath.Sub(7, 2).ToString());

   16 int mul = MyMath.Mul(7,2);

   17 Console.WriteLine(“Multiply = “ + mul );

   18 Console.WriteLine(“Divison = “ + MyMath.Div(7, 2).ToString());

   19 Console.Read();

   20 }

   21 }

   22 }

   23 

Output


Conclusion

In this article, we saw what Static class is and how to work with this class. Thanks for Reading.

Advertisements

One thought on “Back to Basic # Static class in c#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s