C# this关键字

this 关键字引用类的当前实例,还可用作扩展方法的第一个参数的修饰符。

对实例的引用略过,下面给出扩展方法的例子:

 

 1  public static ObservableCollection<T> ArrayToObservableCollection<T>( this T[] list)
 2         {
 3             if (list == null)
 4                 return null;
 5             ObservableCollection<T> collections = new ObservableCollection<T>();
 6             foreach (var item in list)
 7             {
 8                 collections.Add(item);
 9             }
10             return collections;
11         }
12 
13         public static ObservableCollection<T> ListToObservableCollection<T>(this List<T> list)
14         {
15             if (list == null)
16                 return null;
17             ObservableCollection<T> collections = new ObservableCollection<T>();
18             foreach (var item in list)
19             {
20                 collections.Add(item);
21             }
22             return collections;
23         }
24 
25         public static List<T> ObservableCollectionToList<T>(this ObservableCollection<T> col)
26         {
27             if (col == null)
28                 return null;
29             List<T> lst = new List<T>();
30             foreach (var item in col)
31             {
32                 lst.Add(item);
33             }
34             return lst;
35         }

 

posted @ 2014-01-16 16:05  仰望星辰  阅读(180)  评论(0编辑  收藏  举报