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

The difference between C # static variables and non-static variables

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "the difference between C# static variables and non-static variables". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One of the basic concepts of C#, the difference between static variables and non-static variables?

Static variable:

Static variables are declared using the static modifier, created when the class is loaded and accessed through the class. All instances of the class have the same static variable value.

Nonstatic variables:

A variable without a static modifier declaration is called a non-static variable. It is created when a class is instantiated and is accessed through an object. The same non-static variable for different instances of the same class can have different values.

Example:

Using System; using System.Collections.Generic; using System.Text; namespace Example01 {class Program {class Class1 {public static String staticStr = "Class"; public String notstaticStr = "Obj";} static void Main (string [] args) {/ / static variables are accessed through the class, and all instances of this class have the same static variable Console.WriteLine ("Class1's staticStr: {0}", Class1.staticStr); Class1 tmpObj1 = new Class1 () TmpObj1.notstaticStr = "tmpObj1"; Class1 tmpObj2 = new Class1 (); tmpObj2.notstaticStr = "tmpObj2"; / / non-static variables are accessed through objects. The same non-static variable of different objects can have different values Console.WriteLine ("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr); Console.WriteLine ("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr); Console.ReadLine ();}

The second basic concept of C #, the difference between const and static readonly?

Const

Members declared with the const modifier are called constants, which are initialized and embedded in the client program at compile time

Static readonly

Members declared with the static readonly modifier are still variables, except that they are used in a similar way to constants: they are accessed through classes and cannot be modified after initialization. But unlike constants, this variable is initialized at run time

Example:

Test class:

Using System; using System.Collections.Generic; using System.Text; namespace Example02Lib {public class Class1 {public const String strConst = "Const"; public static readonly String strStaticReadonly = "StaticReadonly"; public const String strConst = "Const Changed"; public static readonly String strStaticReadonly = "StaticReadonly Changed";}

Client code:

Using System

Using System.Collections.Generic

Using System.Text

Using Example02Lib

Namespace Example02 {

Class Program {

Static void Main (string [] args)

{

/ / after modifying the strConst initial value of Class1 in Example02, only the Example02Lib project is compiled

/ / then go to Explorer and copy the newly compiled Example02Lib.dll to the directory where Example02.exe is located

Execute Example02.exe

/ / do not debug and run directly in IDE because this will recompile the entire solution!

/ / you can see that the output of strConst has not changed, while the output of strStaticReadonly has changed /

Indicates that the Const variable is initialized and embedded in the client program at compile time, while StaticReadonly is initialized at run time

Console.WriteLine ("strConst: {0}", Class1.strConst)

Console.WriteLine ("strStaticReadonly: {0}", Class1.strStaticReadonly)

Console.ReadLine ()

}

Modified example:

Test class:

Using System; using System.Collections.Generic; using System.Text; namespace Example02Lib {public class Class1 {/ / public const String strConst = "Const"; / / public static readonly String strStaticReadonly = "StaticReadonly"; public const String strConst = "Const Changed"; public static readonly String strStaticReadonly = "StaticReadonly Changed";} "the difference between C# static and non-static variables" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report