Cannot use 'this' in member initializer?

后端 未结 3 963
一整个雨季
一整个雨季 2020-12-11 17:12

Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does:

/// 
/// immutable         


        
3条回答
  •  不知归路
    2020-12-11 18:13

    Your constructor will loop forever, until it pops the stack. This is because it keeps calling itself recursively. Try splitting it up:

    public Pair(TValue1 value1, TValue2 value2)
        : this(value1, value2, () => toStringFunc(this)) { }
    
    public Pair(TValue1 value1, TValue2 value2, Func, String> toStringFunc)
        { /* some actual logic */ }
    

提交回复
热议问题