Back to Basic # Static class in c#

No.of Views741
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:57
Tag : CSharp , Miscellaneous
Back to Basic # Static class in c#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Staticclass
{
public static class MyMath
{

public static int Add(int number1, int number2)
{
return number1 + number2;
}

public static int Sub(int number1, int number2)
{
return number1 - number2;
}
public static int Mul(int number1, int number2)
{
return number1 * number2;
}
public static int Div(int number1, int number2)
{
return number1 / number2;
}


}
}

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.

Image Loading

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Staticclass
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("Sumation= "+ MyMath.Add(7, 2).ToString());
Console.WriteLine("Substraction = " + MyMath.Sub(7, 2).ToString());
int mul = MyMath.Mul(7,2);
Console.WriteLine("Multiply = " + mul );
Console.WriteLine("Divison = " + MyMath.Div(7, 2).ToString());
Console.Read();

}
}
}

 

Output 

Image Loading

Conclusion

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

 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top