-3
所以局部变量,我有了一个Dictionary<string,System.Timers.Timer>
称为“计时器”一类的,我在访问了“定时器”字段的类中的方法:我可以声明直接修改类字段
public static void Save(FastColoredTextBox editor) //this works fine
{
timers[(string)editor.Tag].Dispose();
timers[(string)editor.Tag] = new Timer(Timeout*1000) { AutoReset = false };
timers[(string)editor.Tag].Elapsed += TimerElapsed;
timers[(string)editor.Tag].Start();
}
正如你所看到的,我用timers[(string)editor.Tag]
,因为当我设置这样的变量:
public static void Save(FastColoredTextBox editor) //doesn't modify the Dictionary
{
var timer = timers[(string) editor.Tag];
timer.Dispose();
timer = new Timer(Timeout*1000) { AutoReset = false };
timer.Elapsed += TimerElapsed;
timer.Start();
}
“计时器” 是德作为局部变量分类,并且不修改Dictionary
。
那我能不能设置像将修改类字段Dictionary
第二个例子中的“计时器”局部变量,否则我将在第一个例子中使用直接访问方式是怎样的?
C#没有全局变量。你可以有一个静态字段,但它需要成为某种类型的一部分。 – mason
看看属性或自动属性...也做谷歌搜索和阅读'范围'并了解哪些优先 – MethodMan