问题一:
什么情况下我们使用自动属性功能,生成自动属性时内部没有明确的私有变量那么我们如何访问这个私有变量呢?
如果不能那我们的封装还有什么存在的意义?
Code
1 /**//// <summary>
2 /// net 2.0中使用的方法来定义Email类
3 /// </summary>
4 public class Email20
5 {
6
7 private string _EmailAddress;
8 public string EmailAddress
9 {
10 set {_EmailAddress=value ;}
11 get {return _EmailAddress;}
12 }
13 public static bool IsValid(Email20 _Email)
14 {
15 Regex regex = new Regex(@"^[\w-\.]+@(\w+\.)+[\w-]{2,4}$");
16 return regex.IsMatch(_Email._EmailAddress);
17 }
18 }
19
20
21 //set方法的中间代码
22
23 // .method public hidebysig specialname instance void
24 // set_EmailAddress(string 'value') cil managed
25 //{
26 // // 代码大小 9 (0x9)
27 // .maxstack 8
28 // IL_0000: nop
29 // IL_0001: ldarg.0
30 // IL_0002: ldarg.1
31 // IL_0003: stfld string linq1.Email20::_EmailAddress
32 // IL_0008: ret
33 //} // end of method Email20::set_EmailAddress
34
35 //Get方法的中间代码
36
37 //.method public hidebysig specialname instance string
38 // get_EmailAddress() cil managed
39 //{
40 // // 代码大小 12 (0xc)
41 // .maxstack 1
42 // .locals init ([0] string CS$1$0000)
43 // IL_0000: nop
44 // IL_0001: ldarg.0
45 // IL_0002: ldfld string linq1.Email20::_EmailAddress
46 // IL_0007: stloc.0
47 // IL_0008: br.s IL_000a
48 // IL_000a: ldloc.0
49 // IL_000b: ret
50 //} // end of method Email20::get_EmailAddress
51
52
53
54
55
56
57
58 /**//// <summary>
59 /// net 3.5 中使用方法来定义Email类
60 /// </summary>
61 public class Email35
62 {
63 /**//// <summary>
64 /// net 3.5的属性的简略写法(称其为自动属性 Auto Implemented Properties)
65 /// </summary>
66
67 public string EmailAddress { set; get; }
68
69 //使用扩展方法后这些函数就没必要搁在这里了 见Extensions
70 //public static bool IsValid()
71 //{
72
73 //}
74 }
75
76 //get自动属性生成的中间代码
77
78 //.method public hidebysig specialname instance string
79 // get_EmailAddress() cil managed
80 //{
81 // .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
82 // // 代码大小 11 (0xb)
83 // .maxstack 1
84 // .locals init (string V_0)
85 // IL_0000: ldarg.0
86 // IL_0001: ldfld string linq1.Email35::'<EmailAddress>k__BackingField'
87 // IL_0006: stloc.0
88 // IL_0007: br.s IL_0009
89 // IL_0009: ldloc.0
90 // IL_000a: ret
91 //} // end of method Email35::get_EmailAddress
92
93 //set自动属性生成的中间代码
94
95 //.method public hidebysig specialname instance void
96 // set_EmailAddress(string 'value') cil managed
97 //{
98 // .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
99 // // 代码大小 8 (0x8)
100 // .maxstack 8
101 // IL_0000: ldarg.0
102 // IL_0001: ldarg.1
103 // IL_0002: stfld string linq1.Email35::'<EmailAddress>k__BackingField'
104 // IL_0007: ret
105 //} // end of method Email35::set_EmailAddress
106
107
1 /**//// <summary>
2 /// net 2.0中使用的方法来定义Email类
3 /// </summary>
4 public class Email20
5 {
6
7 private string _EmailAddress;
8 public string EmailAddress
9 {
10 set {_EmailAddress=value ;}
11 get {return _EmailAddress;}
12 }
13 public static bool IsValid(Email20 _Email)
14 {
15 Regex regex = new Regex(@"^[\w-\.]+@(\w+\.)+[\w-]{2,4}$");
16 return regex.IsMatch(_Email._EmailAddress);
17 }
18 }
19
20
21 //set方法的中间代码
22
23 // .method public hidebysig specialname instance void
24 // set_EmailAddress(string 'value') cil managed
25 //{
26 // // 代码大小 9 (0x9)
27 // .maxstack 8
28 // IL_0000: nop
29 // IL_0001: ldarg.0
30 // IL_0002: ldarg.1
31 // IL_0003: stfld string linq1.Email20::_EmailAddress
32 // IL_0008: ret
33 //} // end of method Email20::set_EmailAddress
34
35 //Get方法的中间代码
36
37 //.method public hidebysig specialname instance string
38 // get_EmailAddress() cil managed
39 //{
40 // // 代码大小 12 (0xc)
41 // .maxstack 1
42 // .locals init ([0] string CS$1$0000)
43 // IL_0000: nop
44 // IL_0001: ldarg.0
45 // IL_0002: ldfld string linq1.Email20::_EmailAddress
46 // IL_0007: stloc.0
47 // IL_0008: br.s IL_000a
48 // IL_000a: ldloc.0
49 // IL_000b: ret
50 //} // end of method Email20::get_EmailAddress
51
52
53
54
55
56
57
58 /**//// <summary>
59 /// net 3.5 中使用方法来定义Email类
60 /// </summary>
61 public class Email35
62 {
63 /**//// <summary>
64 /// net 3.5的属性的简略写法(称其为自动属性 Auto Implemented Properties)
65 /// </summary>
66
67 public string EmailAddress { set; get; }
68
69 //使用扩展方法后这些函数就没必要搁在这里了 见Extensions
70 //public static bool IsValid()
71 //{
72
73 //}
74 }
75
76 //get自动属性生成的中间代码
77
78 //.method public hidebysig specialname instance string
79 // get_EmailAddress() cil managed
80 //{
81 // .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
82 // // 代码大小 11 (0xb)
83 // .maxstack 1
84 // .locals init (string V_0)
85 // IL_0000: ldarg.0
86 // IL_0001: ldfld string linq1.Email35::'<EmailAddress>k__BackingField'
87 // IL_0006: stloc.0
88 // IL_0007: br.s IL_0009
89 // IL_0009: ldloc.0
90 // IL_000a: ret
91 //} // end of method Email35::get_EmailAddress
92
93 //set自动属性生成的中间代码
94
95 //.method public hidebysig specialname instance void
96 // set_EmailAddress(string 'value') cil managed
97 //{
98 // .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
99 // // 代码大小 8 (0x8)
100 // .maxstack 8
101 // IL_0000: ldarg.0
102 // IL_0001: ldarg.1
103 // IL_0002: stfld string linq1.Email35::'<EmailAddress>k__BackingField'
104 // IL_0007: ret
105 //} // end of method Email35::set_EmailAddress
106
107
问题2:
查找每个类别中单价最高的产品:(其实这样做是达不到直接显示结果的要求的)
在gridview中没法显示出来。
Code
1var categories =
2 from p in db.Products
3 group p by p.CategoryID into g
4 select new {
5 g.Key,
6 MostExpensiveProducts =
7 from p2 in g
8 where p2.UnitPrice == g.Max(p3 => p3.UnitPrice)
9 select p2
10 };
11
1var categories =
2 from p in db.Products
3 group p by p.CategoryID into g
4 select new {
5 g.Key,
6 MostExpensiveProducts =
7 from p2 in g
8 where p2.UnitPrice == g.Max(p3 => p3.UnitPrice)
9 select p2
10 };
11
问题3:
下面这两行代码怎么得到的结果是一个为什么结怎么有一个Length列呢(只是字符内容的长度)
Code
1var citys = from p in db.Customers select new { p.Country,p.City };
2this.dataGridView1.DataSource = citys;
3
1var citys = from p in db.Customers select new { p.Country,p.City };
2this.dataGridView1.DataSource = citys;
3