Set and Get For Struct and Class In C#

 

代码
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6  namespace SetAndGet
7 {
8 struct Man
9 {
10
11 public Man(string name, string gender):this()
12 {
13 Name = name;
14 Gender = gender;
15 }
16
17 public string Name { get; set; }
18 public string Gender { get; set; }
19 }
20
21 class Woman
22 {
23 public Woman(string name, string gender)
24 {
25 Name = name;
26 Gender = gender;
27 }
28
29 public string Name { get; set; }
30 public string Gender { get; set; }
31 }
32
33 class Program
34 {
35 static void Main(string[] args)
36 {
37 Man man = new Man("Reyes", "man");
38 Console.WriteLine(man.Name);
39 Console.WriteLine(man.Gender);
40
41 Woman woman = new Woman("Sunshine", "woman");
42 Console.WriteLine(woman.Name);
43 Console.WriteLine(woman.Gender);
44
45 Console.Read();
46 }
47 }
48 }
49  

 

如果我们删除了struct中构造函数中的 :this() , 我们将得到一个编译错误: Error 1 Backing field for automatically implemented property 'SetAndGet.Man.Gender'

must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer. 
posted @ 2011-01-20 20:06  Reyes Yang  阅读(417)  评论(0编辑  收藏  举报