Unintuitive behaviour with struct initialization and default arguments

后端 未结 5 2296
小鲜肉
小鲜肉 2021-02-12 17:52
public struct Test 
{
    public double Val;
    public Test(double val = double.NaN) { Val = val; }
    public bool IsValid { get { return !double.IsNaN(Val); } }
}

Te         


        
5条回答
  •  青春惊慌失措
    2021-02-12 18:23

    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.

提交回复
热议问题