閱讀本文大概需要 1.2 分鐘。
          隨著 C# 的升級(jí),C# 在語法上對(duì)對(duì)象的初始化做了不少簡(jiǎn)化,來看看有沒有你不知道的。

          數(shù)組的初始化

          在上一篇羅列數(shù)組的小知識(shí)的時(shí)候,其中也提到了數(shù)組的初始化,這時(shí)直接引用過來。
          int[] arr = new int[3] {1, 2, 3}; // 正兒八經(jīng)的初始化
          int[] arr = new [] {1, 2, 3}; // 簡(jiǎn)化掉了 int 和數(shù)組容量聲明
          int[] arr = {1, 2, 3}; // 終極簡(jiǎn)化
          字典的兩種初始化方式

          第二種是 C# 6 的語法,可能很多人不知道。
          // 方式一:
          var dict = new Dictionary<string, int>
          {
          { "key1", 1 },
          { "key2", 20 }
          };

          // 方式二:
          var dict = new Dictionary<string, int>
          {
          ["key1"] = 1,
          ["key2"] = 20
          };
          含自定義索引器的對(duì)象初始化

          這種初始化原理上其實(shí)是和上面字典的第二種初始化是一樣的。
          public class IndexableClass
          {
          public int this[int index]
          {
          set
          {
          Console.WriteLine("{0} was assigned to index {1}", value, index);
          }
          }
          }

          var foo = new IndexableClass
          {
          [0] = 10,
          [1] = 20
          }
          元組(Tuple)的三種初始化方式

          前面兩種方式很常見,后面一種是 C# 7 的語法,可能有些人不知道。
          // 方式一:
          var tuple = new Tuple<string, int, MyClass>("foo", 123, new MyClass());

          // 方式二:
          var tuple = Tuple.Create("foo", 123, new MyClass());

          // 方式三:
          var tuple = ("foo", 123, new MyClass());
          另外補(bǔ)充個(gè)小知識(shí),在 C# 7 中,元組的元素可以被解構(gòu)命名:
          (int number, bool flage) tuple = (123, true);
          Console.WriteLine(tuple.number); // 123
          Console.WriteLine(tuple.flag); // True
          自定義集合類的初始化

          只要自定義集合類包含Add方法,便可以使用下面這種初始化方式為集合初始化元素。
          class Program
          {
          static void Main()
          {
          var collection = new MyCollection {
          "foo", // 對(duì)應(yīng)方法:Add(string item)
          { "bar", 3 }, // 對(duì)應(yīng)方法:Add(string item, int count)
          "baz", // 對(duì)應(yīng)方法:Add(string item)
          123.45d, // 對(duì)應(yīng)擴(kuò)展方法:Add(this MyCollection @this, double value)
          };
          }
          }

          class MyCollection : IEnumerable
          {
          privatereadonly IList _list = new ArrayList();

          public void Add(string item)
          {
          _list.Add(item);
          }

          public void Add(string item, int count)
          {
          for (int i = 0; i < count; i++)
          {
          _list.Add(item);
          }
          }

          public IEnumerator GetEnumerator()
          {
          return _list.GetEnumerator();
          }
          }

          static class MyCollectionExtensions
          {
          public static void Add(this MyCollection @this, double value) =>
          @this.Add(value.ToString());
          }
          對(duì)象的集合屬性初始化

          我們知道對(duì)集合的初始化必須使用new創(chuàng)建該集合,不能省略,比如:
          // OK
          IList<string> synonyms = new List<string> { "c#", "c-sharp" };

          // 編譯報(bào)錯(cuò),不能省略 new List<string>
          IList<string> synonyms = { "c#", "c-sharp" };
          但如果該集合作為另外一個(gè)類的屬性,則可以省略new,比如:
          public class Tag
          {
          public IList<string> Synonyms { get; set; } = new List<string>();
          }

          var tag = new Tag
          {
          Synonyms = { "c#", "c-sharp" } // OK
          };
          能想到和找到的就這么點(diǎn)了,希望以上會(huì)對(duì)你的編程有所幫助。

          友情鏈接
          ioDraw流程圖
          API參考文檔
          OK工具箱
          云服務(wù)器優(yōu)惠
          阿里云優(yōu)惠券
          騰訊云優(yōu)惠券
          京東云優(yōu)惠券
          站點(diǎn)信息
          問題反饋
          郵箱:[email protected]
          QQ群:637538335
          關(guān)注微信

                宅男噜噜99国产精品麻豆精品 | 国产午夜无码视频在线观看 | 成人h视频免费观看 | 98无码人妻精品一区二区三区 | 欧美无遮挡 |