2013-05-09 24 views
0

當我做編碼我碰到過這樣的函數=>這個關鍵字在某些情況下並不清楚

public RelayCommand(Action<object> execute): this(execute, null) 

我真的不知道了「這個」關鍵詞這裏使用

+0

重複代碼'this'在這方面通常意味着調用*與這些參數這*類另一個構造。這是減少代碼重複的一種方法 – 2013-05-09 03:29:52

回答

5

它的構造函數鏈接。 this(execute, null)調用該類中定義的另一個構造函數,該構造函數需要Action<object>和其他一些值。例如:

class Whatever 
{ 
    public Whatever() : this("string arg") {} // calls Whatever(string) 

    public Whatever(string something) {} 
} 
1

this關鍵字的這種特殊的使用,您可以調用一個構造函數從另一個,大概是爲了提供一個默認的說法。您可以通過應用默認參數值將兩個構造函數「摺疊」爲一個:

public RelayCommand(Action<object> execute, string name = null) { 
    ... 
} 
0

這是指當前構造函數的重載版本。 基本上兩個構造函數鏈接在一起,其可以很好的爲避免在構造

相關問題