C#语法糖: 扩展方法(经常使用)

   

 今天继续分享C#4.0语法糖的扩展方法,这个方法也是我本人比较喜欢的方法。你们先想一想好比咱们之前写的原始类型不能知足如今的需求,而须要在该类型中添加新的方法来实现时你们会怎么作。我先说一下我没有学习到这个知识点以前作的方法:html

最笨的办法就是修改原来的类型,而后添加一个方法来达到需求的变动,以下代码所示:数组

  

复制代码
 1 public  class KuozFF
 2 
 3     {
 4 
 5         public void NormalMethod()
 6 
 7         {
 8 
 9             Console.WriteLine("我是原始方法");
10 
11         }
12 
13 public void ExtensionMethod()
14 
15         {
16 
17             Console.WriteLine("我是扩展方法");
18 
19         }
20 
21     }
复制代码

调用方法:服务器

复制代码
1 KuozFF method=new KuozFF ();
2 
3             method.NormalMethod();
4 
5             method.ExtensionMethod();
6 
7             Console.ReadLine();
复制代码

输出效果以下:ide

可是好比说别人给你的是一个dll文件,你没有办法修改,可是你想在该类中添加你的方法怎么办?post

本身写一个类,而后该类继承自原始类,代码以下:学习

复制代码
 1 public  class KuozFF
 2 
 3     {
 4 
 5         public void NormalMethod()
 6 
 7         {
 8 
 9             Console.WriteLine("我是原始方法");
10 
11         }
12 
13  
14 
15     }
16 
17  
18 
19     public class MYKZFF : KuozFF
20 
21     {
22 
23         public void ExtensionMethod()
24 
25         {
26 
27             Console.WriteLine("我是扩展方法");
28 
29         }
30 
31     }
复制代码

调用代码以下:this

复制代码
1 MYKZFF method=new MYKZFF();
2 
3             method.NormalMethod();
4 
5             method.ExtensionMethod();
6 
7             Console.ReadLine();
复制代码

效果以下:编码

  以上结果能够看出效果是同样的,可是有的人他不想写继承类,也不想修改源代码怎么办?这时候就扩展方法诞生了!我先看看官方解释吧:spa

扩展方法:使你可以向现有类型“添加”方法,而无需建立新的派生类型、从新编译或以其余方式修改原始类型。 扩展方法是一种特殊的静态方法,但能够像扩展类型上的实例方法同样进行调用。 对于用 C# 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差别3d

这是微软MSN上的解释,咱们直接看代码,扩展方法是长什么样吧:

复制代码
 1 public class KuozFF
 2 
 3     {
 4 
 5         public void NormalMethod()
 6 
 7         {
 8 
 9             Console.WriteLine("我是原始方法");
10 
11         }
12 
13  
14 
15     }
16 
17  
18 
19     public static class ExtensionClass
20 
21     {
22 
23         public static void ExtensionMethod(this KuozFF k)
24 
25         {
26 
27             Console.WriteLine("我是扩展方法");
28 
29         }
30 
31     }
复制代码

调用代码以下:

复制代码
1 KuozFF method=new KuozFF();
2 
3             method.NormalMethod();
4 
5             method.ExtensionMethod();
6 
7             Console.ReadLine();
复制代码

输出结果:

从上面代码能够看出咱们客户端调用时无需关心扩展方法在哪儿写的,你只要实例化原始类,扩展方法自动会有的。

扩展方法在C#4.0中是无处不在的,下面咱们看看C#内置的扩展方法来更深入的了解一下:

复制代码
 1 public static class Enumerable
 2 
 3   {
 4 
 5     public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
 6 
 7     public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);
 8 
 9     public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
10 
11     public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);
12 
13 }
复制代码

以上就是微软IEnumerable类的扩展方法,因此咱们平时用的时候方法后面点.后就能出来怎么多丰富的where,select等方法是扩展方法起的做用。这里只是拿出一点扩展方法来展现了一下。

下面咱们写一下string类扩展方法,咱们之前判断一个字符串是否null或空时,用系统内置方法string. IsNullOrEmpty(s),咱们把这个方法作成扩展方法演示一下:

复制代码
 1  public static class Demo1
 2 
 3     {
 4 
 5         public static bool IsNullOrEmpty(this string s)
 6 
 7         {
 8 
 9             return string.IsNullOrEmpty(s);
10 
11         }
12 
13     }
复制代码

调用代码以下:

复制代码
1     string temp = "12";
2 
3             bool result = temp.IsNullOrEmpty();
4 
5             Console.WriteLine(result);
6 
7             Console.ReadLine();
复制代码

输出结果:

从调用代码能够看出string自己是没有IsNullOrEmpty()方法的,经过咱们本身写扩展方法有了该方法。

你们能够在本身的方法或者对系统类的扩展能够写一下,以便在往后的编码过程当中给本身提供方便。


扩展方法注意事项:

一、它至少有一个参数;
二、第一个参数必须附加 this 关键字;
三、第一个参数不能有任何其余修饰符(out/ref)
四、第一个参数不能是指针类型
五、 C# 只支持扩展方法,不支持扩展属性、扩展事件等;
六、  扩展方法的命名空间可使用 namespace System ,但不推荐;
七、  定义扩展方法的类是静态类;

今天就写到这里吧!谢谢朋友们的支持!

 

 

附带  [C#小技巧收集]将字符串转换成List<T>

 

有时须要将一个字符串分隔后,转换成指定类型的数组或List<T>,好比服务器端收到提交的一组checkbox的值,多是一个ID串,相似:56,657,0,1,2,3,4,5,6,7,8,须要将它转成一个int数组或List<T>再进行后续处理。

 

将字符串转换成List<T>中看到了关于这个的讨论,整理以下。

咱们可用Array.ConvertAll 泛型方法来实现,代码以下:

string str = "56,657,0,1,2,3,4,5,6,7,8";
int[] arrInt = Array.ConvertAll<string, int>(str.Split(','), s => int.Parse(s));
foreach (int i in arrInt) Console.WriteLine(i);

 

或者,咱们想用到一些“奇技淫巧”,好比将这功能作成string的扩展方法:

public static List<T> ToList<T>(this string str, char split, Converter<string, T> convertHandler)
{
    if (string.IsNullOrEmpty(str))
    {
        return new List<T>();
    }
    else
    {
        string[] arr = str.Split(split);
        T[] Tarr = Array.ConvertAll(arr, convertHandler);
        return new List<T>(Tarr);
    }
}

调用方法:

List<int> intList = str.ToList<int>(',', s => int.Parse(s));
相关文章
相关标签/搜索