首页 > Windows开发 > 详细

【C#】读书笔记

时间:2015-06-03 19:02:40      阅读:354      评论:0      收藏:0      [点我收藏+]

一,C#对象初始化语法:

1             Product p = new Product()
2             {
3                 Name = "小黄人",
4                 Price = 34,
5                 Description = "机智",
6                 Category = "奢饰品",
7                 ProductID = 0
8             };

声明对象嘛,应该有分号的。。。。

其实,我们在使用集合或数组的时候,早就使用过这种语法糖了,如:

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#】读书笔记

原文:http://www.cnblogs.com/SharpL/p/4549743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!