C#语言进阶——14. C# 的初始化器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#语言进阶——14. C# 的初始化器相关的知识,希望对你有一定的参考价值。

1.C# 的初始化器极其语法

初始化器:就是在一个class或者对象初始化的 对他的属性或者字段赋值,又可以分为对象和集合的初始化器

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //常见初始化方式
 6             var student1 =new StudentName("xiangbing","ke");
 7             //对象初始化器
 8             var student2 = new StudentName { FirstName = "xiangbing", LastName = "ke" };
 9 
10             var student3 = new StudentName("sb", "ke") {ID=100 };
11             Console.WriteLine(student1.ToString());
12             Console.WriteLine(student2.ToString());
13             Console.WriteLine(student3.ToString());
14             Console.ReadLine();
15 
16         }
17 
18         public class StudentName
19         {
20             public string FirstName { get; set; }
21             public string LastName { get; set; }
22             public int ID { get; set; }
23 
24             //构造函数
25             public StudentName()
26             { }
27 
28             public StudentName(string first,string Last)
29             {
30                 FirstName = first;
31                 LastName = Last;
32 
33             }
34 
35             public override string ToString()
36             {
37                 return FirstName + " " + ID;
38             }
39         }
40     }

 

2.C# 的匿名类和初始化器在匿名类中的应用

 1  class Program
 2     {
 3         //在匿名类使用初始化器
 4         static void Main(string[] args)
 5         {
 6             //匿名类,都是只读的,在linq用得很多
 7             var per = new { Age = 10, Name = "keke" };
 8             var Students = new List<StudentName> { new StudentName("ke", "ke"), new StudentName("bin", "bin") };//假如我们有个列表
 9             var StudentFrom = new List<StudentFrom> { new StudentFrom {FirstName="ke",City="putian" }, new StudentFrom {FirstName="me",City="xianyou" } };//假如我们有个列表
10             var joinquery = from s in Students
11                             join f in StudentFrom on s.FirstName equals f.FirstName
12                             select new { FirsName = s.FirstName, LastName = s.LastName, City = f.City };
13             foreach (var item in joinquery)
14             {
15                 Console.WriteLine("{0} {1} {2}",item.FirsName,item.LastName,item.City);
16             }
17 
18 
19 
20             Console.ReadLine();
21 
22         }
23 
24         public class StudentName
25         {
26             public string FirstName { get; set; }
27             public string LastName { get; set; }
28             public int ID { get; set; }
29 
30             //构造函数
31             public StudentName()
32             { }
33 
34             public StudentName(string first,string Last)
35             {
36                 FirstName = first;
37                 LastName = Last;
38 
39             }
40 
41             public override string ToString()
42             {
43                 return FirstName + " " + ID;
44             }
45         }
46 
47         public class StudentFrom
48         {
49             public string FirstName { get; set; }
50             public string City { get; set; }
51         }
52     }

 

3.C# 的集合初始化器

 1  class Program
 2     {
 3         //在匿名类使用初始化器
 4         static void Main(string[] args)
 5         {
 6 
 7             CollectionInitilaizer();
 8 
 9             Console.ReadLine();
10 
11         }
12         private static void CollectionInitilaizer()
13         {
14             //集合列表初始化器
15             var students = new List<StudentName>
16             {
17                 new StudentName {FirstName="ke",LastName="xb",ID=100 },
18                 new StudentName() {FirstName="ks",LastName="nn",ID=101 },
19                 new StudentName("kesb","kesb") { ID=102},
20                 null
21             };
22             foreach (var s in students)
23             {
24                 if (s!=null)
25                 {
26                     Console.WriteLine(s.ToString());
27                 }       
28             }
29 
30             //键值对
31             Dictionary<int, StudentName> studentDic = new Dictionary<int, StudentName>
32             {
33                 { 111,  new StudentName {FirstName="ke",LastName="xb",ID=103 }},
34                 {112, new StudentName() {FirstName="ks",LastName="nn",ID=104 }}
35             };
36             foreach (var s in studentDic)
37             {
38             
39                     Console.WriteLine( s.Value.ToString());
40           
41             }
42         }
43 
44         public class StudentName
45         {
46             public string FirstName { get; set; }
47             public string LastName { get; set; }
48             public int ID { get; set; }
49 
50             //构造函数
51             public StudentName()
52             { }
53 
54             public StudentName(string first,string Last)
55             {
56                 FirstName = first;
57                 LastName = Last;
58 
59             }
60 
61             public override string ToString()
62             {
63                 return FirstName + " " + ID;
64             }
65         }
66 
67         public class StudentFrom
68         {
69             public string FirstName { get; set; }
70             public string City { get; set; }
71         }
72     }

 

以上是关于C#语言进阶——14. C# 的初始化器的主要内容,如果未能解决你的问题,请参考以下文章

C#语言进阶——6.C# 的泛型

C#进阶系列11 泛型

C#进阶系列07 常量和字段

C#中的索引器

C#里类型初始化器和构造方法有啥区别 他们分别在啥情况下用?

C#进阶系列07 方法