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!

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
