您的位置:宽带测速网 > 编程知识 > c#全局变量如何定义及引用

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

2025-06-14 08:59来源:互联网 [ ]

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

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

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

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

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

c#