Why can't I define a default constructor for a struct in .NET?

后端 未结 10 2082
时光说笑
时光说笑 2020-11-22 11:04

In .NET, a value type (C# struct) can\'t have a constructor with no parameters. According to this post this is mandated by the CLI specification. What happens i

10条回答
  •  遥遥无期
    2020-11-22 11:52

    public struct Rational 
    {
        private long numerator;
        private long denominator;
    
        public Rational(long num = 0, long denom = 1)   // This is allowed!!!
        {
            numerator   = num;
            denominator = denom;
        }
    }
    

提交回复
热议问题