Background
In this article we will learn about one of the most reusable object oriented features of C#, static classes. We will learn about them from the basics because I have written this article focusing on students and beginners. Before proceeding further, please refer to my previous articles for a better understanding.
So, let us start from the basics of static classes.
- Created using the static keyword.
- Inside a static class only static members are allowed, in other words everything inside the static class must be static.
- We cannot create an object of a static class.
- A static class cannot be inherited.
- It allows only a static constructor to be declared.
- The methods of a static class can be called using the class name without creating an instance.
- [Access Specifier] [Static Keyword] [Class Template] [ Class Name]
- //class defination
- Public Static Class Customer
- {
- //class defination
- }
- Open Visual Studio from "Start" -> "All programs" -> "Microsoft Visual Studio".
- Then go to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application".
- Then specify the name such as Partial class or whatever name you wish and the location of the project and click on the "OK" button. The new project is created.
- The Solution Explorer will look such as follows:

- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace UsingStaticClass
- {
- class Program
- {
- static string GetName, SetName;//only static members are allowed
- static void Main(string[] args)
- {
- Console.WriteLine("Enter Name");
- Console.WriteLine();
- GetName = Console.ReadLine();
- SetName = Customer.GetCustDet(GetName);//The methods of static class called using the class name
- Console.Clear();
- Console.WriteLine("-------------------------");
- Console.WriteLine(SetName);
- Console.ReadLine();
- }
- }
- public static class Customer //Created using the static keyword
- {
- public static string GetCustDet(string Name) // only static methods are allowed
- {
- return "Your Name is " + Name;
- }
- }
- }
- Download the Zip file from the attachment for the full source code of the application.
In the preceding article, I have briefly explained the use of a static class to make them understandable to beginners and newcomers. If you have any suggestion regarding this article then please contact me.

Vithal WadjePosted Jun 9, 2014, 1:35 PM
thanks to all
Sathya NarayanPosted Jun 9, 2014, 9:36 AM
Good Article
Nimit JoshiPosted Jun 9, 2014, 7:59 AM
Great for the beginners..
Prerana TiwariPosted Jun 9, 2014, 6:09 AM
Nice one....