Unintuitive behaviour with struct initialization and default arguments

后端 未结 5 1708
半阙折子戏
半阙折子戏 2021-02-12 17:46
public struct Test 
{
    public double Val;
    public Test(double val = double.NaN) { Val = val; }
    public bool IsValid { get { return !double.IsNaN(Val); } }
}

Te         


        
5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 18:28

    Because a struct can't have a user-defined parameterless constructor.

    Test(double val = double.NaN) looks like one, but it's actually compiled as Test(double val) with some metadata about the default value.

提交回复
热议问题