簡體   English   中英

C#引用具有相同名稱的局部變量的靜態字段

[英]C# referencing static field with like-named local variable

在相當有限的上下文中,我需要從構造函數引用靜態類字段,該構造函數具有與靜態字段同名的變量。 下面是一個示例,其中還包括一個非靜態字段,以突出顯示對於非靜態字段,可以使用“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”一樣? 在完美的世界中,我只是重構類變量或縮限變量以具有不同的標識符,但是出於技術原因(打印文檔),這里都不能更改。

謝謝。

完全限定構造函數中的靜態變量名稱。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM