很遗憾的是 C# 语言中没有全局变量!不知道设计的时候是怎么考虑的?滥用全局变量可能会引起程序的混乱,但是合理使用,也会带来很大的方便。
幸运的是,我们可以使用类里面的 static 字段曲折地达到使用“全局变量”的目的。
例如以下代码:
//文件 global.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace MyGlobalNameSpace
{
public class myGlobal
{
private static string var1 = "";
public static string sVar1
{
get
&n