Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use static members of C# class

Shulou Source: shulou.com Published: 2022-05-31 13:40:42 09月20日 Update

Most people do not understand the knowledge points of this article "how to use static members of C# class", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this article "how to use static members of C# class" article.

Static members of the C# class

We can use the static keyword to define class members as static. When we declare a class member static, it means that no matter how many objects of the class are created, there will be only one copy of the static member.

The keyword static means that there is only one instance of that member in the class. Static variables are used to define constants because their values can be obtained by calling the class directly without creating an instance of the class. Static variables can be initialized outside the definition of a member function or class. You can also initialize static variables within the definition of the class.

The following example demonstrates the use of static variables:

Using System

Namespace StaticVarApplication

{

Class StaticVar

{

Public static int num

Public void count ()

{

Num++

}

Public int getNum ()

{

Return num

}

}

Class StaticTester

{

Static void Main (string [] args)

{

StaticVar S1 = new StaticVar ()

StaticVar S2 = new StaticVar ()

S1.count ()

S1.count ()

S1.count ()

S2.count ()

S2.count ()

S2.count ()

Console.WriteLine (variable num of "S1: {0}", s1.getNum ())

Console.WriteLine (variable num of "S2: {0}", s2.getNum ())

Console.ReadKey ()

}

}

}

When the above code is compiled and executed, it produces the following results:

S1 variable num: 6s2 variable num: 6 above is about the content of this article "how to use static members of C# class", I believe we all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Tags: Static member variable content Category instance keyword meaning article knowledge article face code value function copy only can be passed can be found in Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Linux Redmi Docker