C#引用具有相同名称的局部变量的静态字段

时间:2011-11-27 13:22:44

标签: c# scope

在一个相当有限的上下文中,我需要从构造函数引用一个静态类字段,该构造函数具有一个与静态字段同名的变量。下面是一个示例,其中还包括一个非静态字段,以突出显示对于非静态字段,可以使用“this”来引用类字段:

public class Example () {

    private static DateTime firstInstance;
    private static DateTime referenceInstance;

    private String Name;

    static Example() {
        first=DateTime.Now;
    }

    public Example(String Name, DateTime referenceInstance) {
        this.Name=Name;
        referenceInstance=referenceInstance;
    }
}

如何在不使用“this”关键字的情况下访问“referenceInstance”静态字段,如同使用“Name”一样?在一个完美的世界中,我只是重构类变量或缩限变量以具有不同的标识符,但是出于技术原因(打印文档),这里都不能更改。

感谢。

1 个答案:

答案 0 :(得分:2)

完全限定构造函数中的静态变量名。

public Example(String Name, DateTime referenceInstance) {
 this.Name=Name;
 Example.referenceInstance=referenceInstance;
}