Home  >  Article  >  Backend Development  >  Static Class in C#

Static Class in C#

王林
王林Original
2024-09-03 15:32:301188browse

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:

Static Class in C#

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 :

Static Class in C#

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:

  1. They cannot have any of the access modifiers.
  2. They cannot be defined along with arguments.
  3. 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#

  1. We will get an error in case you, we any of the members as a non-static member.
  2. 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.
  3. The static keyword is used before the class keyword in the class definition to declare a static class.
  4. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# Data Grid ViewNext article:C# Data Grid View