【C#】讀書筆記

來源:互聯網
上載者:User

標籤:

一,C#對象初始化文法:

1             Product p = new Product()2             {3                 Name = "小黃人",4                 Price = 34,5                 Description = "機智",6                 Category = "奢飾品",7                 ProductID = 08             };

聲明對象嘛,應該有分號的。。。。

其實,我們在使用集合或數組的時候,早就使用過這種文法糖了,如:

1 List<int> tempList = new List<int> { 1, 2, 3 };

 二、使用拓展方法

第三方類或者是沒有原始碼的類,可以通過用拓展方法來獲得所需的功能。

 1      public static class ProductListExtension 2     { 3         public static decimal TotalPrice(this ProductList list) 4         { 5             decimal d = 0; 6             foreach (Product item in list.Products) 7             { 8                 d += item.Price; 9             }10             return d;11         }12     }

注意,拓展方法所在的類必須是靜態類,拓展方法也必須是靜態方法。

調用的方法,與普通的方法使用方法一致,如下:

1 decimal temp = p.TotalPrice();

 

【C#】讀書筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.