In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members and static methods. It is not allowed to create objects of the static class and since it does not allow to create objects it means it does not allow instance constructor. Static classes are sealed, means you cannot inherit a static class from another class.
Syntax:
static class Class_Name
{
// static data members
// static method
}
In C#, the static class contains two types of static members as follows:
- Static Data Members: As static class always contains static data members, so static data members are declared using static keyword and they are directly accessed by using the class name. The memory of static data members is allocating individually without any relation with the object.
Syntax:
static class Class_name
{
public static nameofdatamember;
}
- Static Methods: As static class always contains static methods, so static methods are declared using static keyword. These methods only access static data members, they can not access non-static data members.
Syntax:
static class Class_name {
public static nameofmethod()
{
// code
}
}
Example 1:
C#
// C# program to illustrate the
// concept of static class
using System;
namespace ExampleOfStaticClass {
// Creating static class
// Using static keyword
static class Author {
// Static data members of Author
public static string A_name = "Ankita";
public static string L_name = "CSharp";
public static int T_no = 84;
// Static method of Author
public static void details()
{
Console.WriteLine("The details of Author is:");
}
}
// Driver Class
public class GFG {
// Main Method
static public void Main()
{
// Calling static method of Author
Author.details();
// Accessing the static data members of Author
Console.WriteLine("Author name : {0} ", Author.A_name);
Console.WriteLine("Language : {0} ", Author.L_name);
Console.WriteLine("Total number of articles : {0} ",
Author.T_no);
}
}
}
OutputThe details of Author is:
Author name : Ankita
Language : CSharp
Total number of articles : 84
Example 2:
C#
// C# program to demonstrate
// the concept of static class
using System;
// declaring a static class
public static class GFG {
// declaring static Method
static void display()
{
Console.WriteLine("Static Method of class GFG");
}
}
// trying to inherit the class GFG
// it will give error as static
// class can't be inherited
class GFG2 : GFG {
public static void Main(String[] args) {
}
}
Compile Time Error:
prog.cs(20,7): error CS0709: `GFG2': Cannot derive from static class `GFG'
Explanation: In the above example, we have a static class named as Author by using static keyword. The Author class contains static data members named A_name, L_name, and T_no, and a static method named as details(). The method of a static class is simply called by using its class name like Author.details();. As we know that static class doesn't consist object so the data member of the Author class is accessed by its class name, like Author.A_name, Author.L_name, and Author.T_no.
Difference between static and non-static class
|
Static class is defined using static keyword. | Non-Static class is not defined by using static keyword. |
In static class, you are not allowed to create objects. | In non-static class, you are allowed to create objects using new keyword. |
The data members of static class can be directly accessed by its class name. | The data members of non-static class is not directly accessed by its class name. |
Static class always contains static members. | Non-static class may contain both static and non-static methods. |
Static class does not contain an instance constructor. | Non-static class contains an instance constructor. |
Static class cannot inherit from another class. | Non-static class can be inherited from another class. |
Similar Reads
Static keyword in C# static is a modifier in C# which is applicable for the following: ClassesVariablesMethodsConstructorIt is also applicable to properties, event, and operators. To create a static member(class, variable, methods, constructor), precede its declaration with the keyword static. When a member is declared
4 min read
C# Program to Demonstrate the Static Constructor in Structure A Structure is a well-defined data structure that can hold elements of multiple data types. It is similar to a class because both are user-defined data types and both contain a bunch of different data types. You can also use pre-defined data types. However, sometimes the user might be in need to def
2 min read
C# | Difference between Static Constructors and Non-Static Constructors Prerequisite: Constructors in C# Static constructors are used to initialize the static members of the class and are implicitly called before the creation of the first instance of the class. Non-static constructors are used to initialize the non-static members of the class. Below are the differences
6 min read
C# Program to Get the Count of Total Created Objects C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, we will create multip
3 min read
C++ Static Data Members Static data members are class members that are declared using static keywords. A static member has certain special characteristics which are as follows:Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
5 min read