Esempio n. 1
0
        private static void ObjectInitializer()
        {
            var student1 = new StudentName("QI", "Xing");
            var student2 = new StudentName {
                FirstName = "QI", LastName = "Xing"
            };
            var student3 = new StudentName {
                ID = 100
            };
            var student4 = new StudentName("Wu", "Shuang")
            {
                ID = 101
            };

            Console.WriteLine(student1.ToString());
            Console.WriteLine(student2.ToString());
            Console.WriteLine(student3.ToString());
            Console.WriteLine(student4.ToString());
        }
Esempio n. 2
0
        private static void ObjectInitializer()
        {
            var student1 = new StudentName("MeiMei", "Wang");
            //先调用无参的构造函数
            var student2 = new StudentName()
            {
                FirstName = "MeiMei", LastName = "Wang"
            };
            var student3 = new StudentName()
            {
                ID = 103
            };
            var student4 = new StudentName("Lei", "Li")
            {
                ID = 104
            };

            Console.WriteLine(student1);
            Console.WriteLine(student2);
            Console.WriteLine(student3);
            Console.WriteLine(student4);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            // 初始化器实现方法
            var student1 = new StudentName("Meimei", "Wang");//调用的是构造函数是public StudentName(string first,string last)
            var student4 = new StudentName("Meimei", "Wang")
            {
                ID = 101
            };                                                            //调用的是构造函数是public StudentName(string first,string last)
            var student2 = new StudentName()
            {
                FirstName = "Meimei", LastName = "Wang", ID = 101
            };                                                                                     //调用的是构造函数是public StudentName()
            var student3 = new StudentName {
                FirstName = "Meimei", LastName = "Wang", ID = 101
            };                                                                                   //调用的是构造函数是public StudentName()

            // 匿名类的初始化器
            // 匿名类的属性是只读的
            var    pet  = new { Age = 10, Name = "Meimei" };
            string name = pet.Name;
            //pet.Name = "无法更改";

            // 匿名类初始化器常用于Linq语句
            List <StudentName> slist = new List <StudentName>();
            var query = from n in slist
                        select new { FirstName = n.FirstName, LastName = n.LastName, Id = n.ID };

            // 集合初始化器
            var students = new List <StudentName>()
            {
                new StudentName("Meimei", "Wang"),
                new StudentName("Meimei", "Wang")
                {
                    ID = 101
                },
                new StudentName()
                {
                    FirstName = "Meimei", LastName = "Wang", ID = 101
                },
                new StudentName {
                    FirstName = "Meimei", LastName = "Wang", ID = 101
                },
                null //还可以包含null
            };

            Dictionary <int, StudentName> studentsDic = new Dictionary <int, StudentName>()
            {
                { 101, new StudentName("Meimei", "Wang") },
                { 102, new StudentName("Meimei", "Wang")
                  {
                      ID = 101
                  } },
                { 103, new StudentName()
                  {
                      FirstName = "Meimei", LastName = "Wang", ID = 101
                  } },
                { 104, new StudentName {
                      FirstName = "Meimei", LastName = "Wang", ID = 101
                  } }
            };

            foreach (var item in studentsDic)
            {
                // item.Value就是一个StudentName对象
                Console.Write(item.Value);
            }
        }