C#3.0特性---自动属性---构造函数需要显示的调用无参数的构造函数

C#3.0特性---自动属性---构造函数需要显示的调用无参数的构造函数

在结构体中使用自动属性时候,需要注意,所有构造函数都需要显示的调用无参数的构造函数 this() ,否则,会出现编译错误;因为只有这样,编译器才能知道所有字段都已经被赋值了;然而,类却不需要显示的调用无参数的构造函数,这主要由编译器设计决定,记住就好了;

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace CurrentNamespace
 8 {
 9     public struct TestPerson
10     {
11         public string Name { get; set; }
12         public int Age { get; private set; }
13 
14         //结构体中,不显示的调用无参构造函数this()时候,会出现,编译错误
15         public TestPerson(string name)
16             //: this() 如果不显示的调用,将会保错
17         {
18             this.Name = name;
19         }
20     }
21 }

 

posted @ 2015-11-21 16:33  t800  阅读(424)  评论(0)    收藏  举报