[C#3.0] 物件初始設定式(Object Initializers)

  • 5535
  • 0

[C# 3.0] 物件初始設定式(Object Initializers)

Introduction

物件初始設定式可讓您在建立期間將值指派給物件的任何可存取的欄位或屬性,而不用明確叫用建構函式。

有人說這是為了搭配 LINQ 所開發出來的特性。

詳細說明可參考

 

Example

sample1

   class GirlFriend {
        //自動實作的屬性
        //可參考 http://msdn.microsoft.com/zh-tw/library/bb384054.aspx
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Boy {
        public string Name { get; set; }
        public int Age { get; set; }
        public GirlFriend Girl { get; set; }
    }

    class Program {
        static void Main(string[] args) {

            GirlFriend gf = new GirlFriend();
            gf.Name = "花兒";
            gf.Age = 19;
            Console.WriteLine("{0} : {1}={2}, {3}={4}", "gf", "gf.Name", gf.Name.ToString(), "gf.Age", gf.Age.ToString());

            //用物件初始化器建立物件
            GirlFriend gf2 = new GirlFriend { Name = "瓶兒",Age = 21 };
            Console.WriteLine("{0} : {1}={2}, {3}={4}", "gf2", "gf2.Name", gf2.Name.ToString(), "gf2.Age", gf2.Age.ToString());

            Boy by = new Boy();
            by.Name = "瀟灑哥";
            by.Age = 25;
            by.Girl = new GirlFriend { Name = "甜兒", Age = 21 };
            Console.WriteLine("{0} : {1}={2}, {3}={4}, {5}={6}, {7}={8}", "by", "by.Name", by.Name.ToString(), "by.Age", by.Age.ToString(),
                                "by.Girl.Name", by.Girl.Name, "by.Girl.Age", by.Girl.Age.ToString());

            //用物件初始化器建立物件
            Boy by2 = new Boy { Name = "番薯哥", Age = 30, Girl = new GirlFriend { Name = "小雨", Age = 18 } };
            Console.WriteLine("{0} : {1}={2}, {3}={4}, {5}={6}, {7}={8}", "by2", "by2.Name", by2.Name, "by2.Age", by2.Age,
                                "by2.Girl.Name", by2.Girl.Name, "by2.Girl.Age", by2.Girl.Age);
            Console.ReadKey();            

        }
    }

輸出結果

2010-01-20_213553

 

sample2

    class Girlfriend {
        public string Name { get; set; }
        public int Age { get; set; }
    }

   
    class Boy {
        public string Name { get; set; }
        public int Age { get; set; }

        /// <summary>
        /// 集合內容
        /// </summary>
        public List<Girlfriend> Girls { get; set; }
    }
    class Program {
        static void Main(string[] args) {
            //用集合初始化器建立集合
            List<string> gfs1 = new List<string> { "小小", "大大", "玩玩" };

            Console.WriteLine("gfs1 : " + gfs1[0].ToString() + " " + gfs1[1].ToString() + " " + gfs1[2].ToString());

            //等價於
            List<string> gfs2 = new List<string>();
            gfs2.Add("小小2");
            gfs2.Add("大大2");
            gfs2.Add("玩玩2");

            Console.WriteLine("gfs2 : " + gfs2[0].ToString() + " " + gfs2[1].ToString() + " " + gfs2[2].ToString());

            //物件集合初始化器復合使用
            Boy boy = new Boy {
                Name = "林晚榮",
                Age = 24,
                Girls = new List<Girlfriend> 
                                    { 
                                        new Girlfriend { Name = "肖青漩", Age = 23 }, 
                                        new Girlfriend { Name = "董巧巧", Age = 18 }, 
                                        new Girlfriend { Name = "蕭玉霜", Age = 16 } 
                                    }
            };

            Console.WriteLine("boy : " + boy.Name + " " + boy.Age);
            Console.WriteLine("girl1 : " + boy.Girls[0].Name + " " + boy.Girls[0].Age);
            Console.WriteLine("girl2 : " + boy.Girls[1].Name + " " + boy.Girls[1].Age);
            Console.WriteLine("girl3 : " + boy.Girls[2].Name + " " + boy.Girls[2].Age);
            Console.ReadKey();
        }
    }

 

 

輸出結果

2010-01-20_214627

 

 

Link

 

程式碼下載 DemoInitializer1.rar

                     DemoInitializer2.rar

三小俠  小弟獻醜,歡迎指教