A static class is a class that we cannot instantiate. The only and most important objective of the static class is to give over blueprints of the inherited classes. It is created with the help of the “static” keyword in C#. The static class contains static members only. We cannot create the object for the static class. In this topic, we are going to learn about Static Class in C#.
The static class in C# consists of two types of static which are illustrated below :
Static data members are declared by the usage of the static keyword since the static class always contains the static data members. They are also directly accessed by using the class name. The memory of the static data members is allocated individually irrespective of its relationship with the object.
Syntax:
static class NameOfClass { public static name_of_datamember; }
Example :
public class Vehicle { public static int Wheels = 4; public static int GasTank { get { return 23; } } public static void move() { } public static event EventType RunOutOfGas; // Extra non-static fields as well as properties }
They get initialized before the static member gets accessed for the first time and before the static constructor if one is called. To access it, we make use of the name of the class rather than a variable name.
The usage of the static keyword declares static methods since the static class always contains static methods. These methods can access only the static data members and cannot access non-static data members.
Syntax:
static class name_of_class { public static name_of_method() { // code } }
Here are the following examples mention below
Code:
/* * C# Program to Check whether the Entered Number is Even or Odd */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace check1 { class EvenAndOdd { static void Main(string[] args) { int i; if (4 % 2 == 0) // You can enter any number you wish to check for even / odd { Console.Write("Entered Number is an Even Number"); Console.Read(); } else { Console.Write("Entered Number is an Odd Number"); Console.Read(); } } } }
Output:
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; public static class ABC { // declaration of static Method static void details() { Console.Write("Static Method of the class ABC is"); } } // Inheritance of the class ABC which would give an error since static // class cannot be inherited class ABC2 : ABC { public static void Main(String[] args) { } }
Output :
Explanation: In the first example, there is a static class named Book by using the static keyword. Book class consists of static data members who are name, l, and t, and also a static method named specs(). This method of the static class is called by using the class name, that is, Book. specs();. Since we are already aware that the static class doesn’t consist of objects, so data members of the Book class are accessed by using its class name, that is, Book.name, Book. l and Book.t.
Static Constructors
Static constructors are basically useful in the initialization of the static data members, as compared to the normal constructor, that is, the non-static constructor that is useful in the initialization of the non-static data members.
Features/Rules:
You must be knowing that the basic components of the application’s memory are heap and stack. A special area inside the heap is called a High-Frequency Heap wherein static members are stored. Static members that are of non-static classes as well are stored in a heap, and then they are shared across all of the instances of the class. Therefore the changes done by one instance get reflected in all of the other instances.
As you must be already knowing, the static member can contain only other of the static members since static members get invoked regardless of the creation of an instance. Henceforth, they cannot access non-static members.
The above is the detailed content of Static Class in C#. For more information, please follow other related articles on the PHP Chinese website!