温馨提示×

c#全局变量如何定义及引用

c#
小亿
109
2024-03-01 10:11:14
栏目: 编程语言

C#中全局变量可以通过使用static关键字来定义,全局变量可以在整个程序中使用,例如:

public class GlobalVariables
{
    public static int globalInt = 10;
    public static string globalString = "Hello";
}

要引用全局变量,可以直接使用类名来访问,例如:

int myInt = GlobalVariables.globalInt;
string myString = GlobalVariables.globalString;

这样就可以在任何地方引用全局变量globalIntglobalString

0