c#的扩展方法

来源:互联网 发布:谷嫂淘宝同款排除王 编辑:程序博客网 时间:2024/04/24 21:33

简单点说就是给一个类增加点方法


例子【别处的】:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace KuoZhanFun                      //指定命名空间{    public static class Program              //声明扩展方法的静态类    {        public static int ToInt32WithD(this string str,int d)                                    //声明扩展方法,并指定扩展类型为string,亲,必须为静态方法奥        {            return (int)((double)Int32.Parse (str)/d);        }    }    public class Test    {        static void Main(string[] args)        {            string s = "123456";            int t = 10;            //调用扩展方法ToInt32WithD(this string str,int d)            int v = s.ToInt32WithD(t);            Console.WriteLine(v);            Console.ReadLine();        }    }}


0 0
原创粉丝点击