C#扩展方法的使用

    在平常的开发中,可能会遇到一些问题需要一个特殊的方法去处理一下,有些是系统中存在的,可以直接调用,然后处理后封装成一个方法,但是有些方法系统中没有,那么就需要自定义扩展方法来供自己调用;

     简单案例如下:

(1)调用系统方法进行排序数组

        /// <summary>
        /// 排序
        /// </summary>
        private static void Orderby()
        {
            //实现排序  语法糖 扩展方法
            int[] ints = { 1, 4, 9, 2, 4, 90, 80, 29 };
            var result = ints.OrderByDescending(g => g);
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }

(2)定义一个扩展方法进行统计字段串中的单词数量

        /// <summary>
        /// 扩展方法的使用
        /// </summary>
        public static void StringCount()
        {
            string s = "This is a C# Language !";
            int i = s.WordCount();//调用扩展方法
            Console.WriteLine(i);
        }

 在这里WordCount()为自定义的一个方法:步骤如下:

 1.新建一个静态类

 2.在静态类里面定义一个静态的方法

 3.参数为 :this+参数类型+参数

     

(3)自定义方法操作枚举

     1.首先要定义一个枚举

     2.在静态类里面定义静态方法

     3.主程序调用

 

静态类代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 扩展方法在Linq中的使用
 8 {
 9     //扩展方法 1.必须在静态类上面 2.必须是静态的方法
10     public static class StringExtension
11     {
12         //统计一句话中有几个单词
13         public static int WordCount(this string str)//语法:this+类型名称 可加其它具体参数
14         {
15             //Split() 第一个参数表示根据空格 . 或!进行分割  第二个参数表示去除空项
16             return str.Split(new char[] { ' ', '.', '!' }, StringSplitOptions.RemoveEmptyEntries).Length;
17         }
18 
19         /// <summary>
20         /// 对枚举类型添加扩展方法
21         /// </summary>
22         public static Grades minPassing = Grades.D;
23         public static bool Passing(this Grades grade)
24         {
25             return grade >= minPassing;
26         }
27 
28     }
29 }
View Code

 

主程序代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 扩展方法在Linq中的使用
 8 { 
 9     //扩展方法在枚举类型中的使用
10     public enum Grades {F=0,D=1,C=2,B=1,A=0 };
11     class Program
12     {
13         static void Main(string[] args)
14         {
15 
16             Orderby();
17             StringCount();
18 
19             var g1 = Grades.D;
20             var f = Grades.F;
21             var c = Grades.C;
22             Console.WriteLine("是否通过:{0}",g1.Passing()?"Yes":"NO");
23             Console.WriteLine("是否通过:{0}", f.Passing() ? "Yes" : "NO");
24             Console.WriteLine("是否通过:{0}", c.Passing() ? "Yes" : "NO");
25             StringExtension.minPassing = Grades.C;
26             Console.WriteLine("是否通过:{0}", g1.Passing() ? "Yes" : "NO");
27             Console.WriteLine("是否通过:{0}", f.Passing() ? "Yes" : "NO");
28             Console.WriteLine("是否通过:{0}", c.Passing() ? "Yes" : "NO");
29 
30             Console.ReadLine();
31 
32         }
33 
34         /// <summary>
35         /// 排序
36         /// </summary>
37         private static void Orderby()
38         {
39             //实现排序  语法糖 扩展方法
40             int[] ints = { 1, 4, 9, 2, 4, 90, 80, 29 };
41             var result = ints.OrderByDescending(g => g);
42             foreach (var item in result)
43             {
44                 Console.WriteLine(item);
45             }
46         }
47 
48         /// <summary>
49         /// 扩展方法的使用
50         /// </summary>
51         public static void StringCount()
52         {
53             string s = "This is a C# Language !";
54             int i = s.WordCount();//调用扩展方法
55             Console.WriteLine(i);
56         }
57 
58 
59 
60 
61 
62     }
63 }
View Code

 

posted @ 2016-08-04 00:04  微笑代表淡定.Net  阅读(106)  评论(0)    收藏  举报