Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why static class
3.
Static Class members
3.1.
Static data member
3.2.
Static Methods
3.3.
Examples
4.
Difference between static and non-static class
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Static Class in C#

Author Ankit Kumar
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

A static class cannot be instantiated. Instantiation is when a new instance of the class is created (an object). Or in other words, the new operator cannot be used to create a variable of the class type. Well, this is a major difference that separates static class and non-static class. 

Static class only has static data members, static variables, static methods. Static classes do not support inheritance and no instance constructors as of regular classes. So, no object for static class can be created. Static classes can have one static parameterless constructor that cannot be inherited. 

Let's see the syntax of static class declaration.

static class classname 

 //static methods 

 //static data members 

Now, we'll see why it's used.

Why static class

Static classes are used in cases where we need some utility functions and Properties. Utility functions are prevalent in some software, and everyone in that particular software uses those functions. So it does not make sense to create an object for such a class that many programmers use from time to time—for example, Built-in Console Class of C#.

A static class can make implementation simpler, safer, and faster because we do not have to create an object to invoke its methods. A static class can be useful as a container for groups of methods that only operate on input arguments and do not need to retrieve or set any internal instance fields.

Now it’s time to discuss the types of static members of the static class.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Static Class members

The members of a class may be qualified as static. Static methods, fields, attributes, and events can all be found in a non-static class. The static member of a class can be called even if no instance of the class has been created. The class name, not the instance name, is always used to access the static member. Regardless of how many instances of the class are produced, only one copy of a static member exists. 

Static methods and properties can't access non-static fields and events in their contained type. They can't access any object's instance variables unless they're explicitly provided as a method argument. Static fields are commonly used to maintain track of the number of objects that have been created or to hold a value that must be shared by all instances.

Following are the static members:

Static data member

A static data member of the class is just like a global variable for its class. This data member is globally available for all the objects of that class type. The static data members are usually maintained to store values common to the entire class. For instance, a class may have a static data member keeping track of its number of existing objects.

Now we'll see the declaration of static data members.

static class classname 

{

    public static name_of_datamember;

}

Static Methods

A member method that access is only the static members of a class may be declared as a static method. Since static methods belong to the class and not to any instance of the class, they can be overloaded but not overridden. Keyword static is put before the method declaration in the class definition.

static class classname

{

    public static name_of_method()

    {

         .

         .

     }

}

Now it’s time to understand the concept of static class by learning from examples.

Examples

We’ll see an example of a static class. Firstly we declare a static class and then declare static variables and initialise them. Then we’ll declare the static methods and initialise them. In the end, we’ll have the output in which we get to know about the Ninjas solved problems.

// C# program to show the concept of static class
using System;
namespace ex_static_class {
// Creating static class
// Using static keyword
static class Ninja {
// Static data members of Ninja
public static string name = "Ankit";
public static string dom = "Coding Ninjas Studio";
public static int no = 99;
// Static method of Ninja
public static void details()
{
Console.WriteLine("The details of Ninja is:");
}
}
// Driver Class
public class CodingNinjas { 
// Main Method
static public void Main()
{
// Calling static method of Ninja
Ninja.details();
// Accessing the static data members of Ninja
Console.WriteLine("Ninja name: {0} ", Ninja.name);
Console.WriteLine("Platform: {0} ", Ninja.dom);
Console.WriteLine("Total number of problems solved: {0} ",Ninja.no);
}
}
}

Output:

The details of Ninja is:

Ninja name: Ankit 

Platform: Coding Ninjas Studio 

Total number of problems solved: 99 

The method of a static class is called by using its class name like Ninja.details();. As we know, that static class doesn't consist of an object, so the data member of the Ninja class is accessed by its class name, like Ninja.name, Ninja.dom, and Ninja.no. Now we'll see the difference between static and non-static classes.

Also see,  Ienumerable vs Iqueryable

Difference between static and non-static class

Static Class Non-Static Class
The static keyword is used to define No, any special keyword is required
The creation of new objects is not allowed The creation of new objects is allowed using the "new" keyword
The data members of a static class can be accessed directly using the name of the class The data members of a non-static class can not be accessed directly using the name of the class.
It will always have static members only It can have static as well as non-static members
There is no instance constructor in the static class There is an instance constructor in the non-static class
Inheritance is prohibited Inheritance is allowed

Now it’s time to munch on some FAQs.

FAQs

  1. What is instantiation?
    Instantiation is when a new instance of the class is created (an object). Or in other words, the new operator is used to create a variable of the class type.
  2. When static classes are used?
    Static classes are used in cases where we need some utility functions and properties. A static class can make implementation simpler, safe and faster because we do not have to create an object to invoke its methods. A static class can be useful as a container for groups of methods that only operate on input arguments and do not need to retrieve or set any internal instance fields.
  3. What are static data members?
    A static data member of the class is just like a global variable for its class. This data member is globally available for all the objects of that class type. The static data members are usually maintained to store values common to the entire class.
  4. What is a static method?
    A member method that access is only the static members of a class may be declared as a static method. Since static methods belong to the class and not to any instance of the class, they can be overloaded but not overridden.
  5. What is the major difference between static and non-static classes?
    In static class, neither inheritance is allowed, nor the creation of an object is allowed, while in non-static classes, both inheritances, as well as the creation of object using the new keyword, are allowed.

Let’s summarise our learning.

Also check out this article - singleton design pattern in c#

Key Takeaways

In this article, we have extensively discussed C# static classes. We got to know why static classes are used. We learnt what the static data members are. Then we learnt about static data methods. Then we learnt about the static and non-static class differences. In the end, we went through some FAQs. Ninjas! Check out our other articles on C# to master your learnings in this.

We hope that this blog has helped you enhance your knowledge regarding Type conversion in C# and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass