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#.
Static Members
The static class in C# consists of two types of static which are illustrated below :
1. Static Data Members
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.
2. Static Methods
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 } }
Examples of Static Class in C#
Here are the following examples mention below
Example #1
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:
Example #2
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:
- They cannot have any of the access modifiers.
- They cannot be defined along with arguments.
- They do not have access to non-static data members.
Memory Allocation for Static Items
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.
Advantages of Static Class in C#
- We will get an error in case you, we any of the members as a non-static member.
- Again a compile-time error is generated in case we try to create an instance to static class since static members can be accessed directly along with their class name.
- The static keyword is used before the class keyword in the class definition to declare a static class.
- Static class members can be accessed by class name that is followed by member name.
Conclusion
- We cannot instantiate the static classes using the new keyword
- Static items only have the ability to access other static items. Consider that static class contains only static members like variables, methods, etc.
- A static method only contains static variables, and also they can only access the rest of the static items.
- Static items have the capability to share resources among multiple users.
- We cannot use static along with indexers, destructors, or the types that are other than the classes.
- Additionally, a static constructor in the non-static class will run only one time when the class gets instantiated for the first time.
- Also, a static constructor present in the static class will run only one time whenever any of the static members is accessed for the first time.
- Static members get allocated in a high-frequency heap area of memory.
The above is the detailed content of Static Class in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how?Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.
