Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use static members of C# class

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report