Linq扩展方法

编译器转换linq查询,已调用方法而不是LINQ查询。LINQ为IEnumerable<T>接口提供了各种扩展方法,以便用户在实现了该接口的任意集合上使用LINQ查询。扩展方法在静态类中的声明,定义为一个静态方法,其中第一个参数定义了它扩展类型。

扩展方法可以将方法写入最初没有提供该方法的类中。还可以把方法添加到实现某个特定接口的任何类中,这样多个类就可以使用相同的实现代码。

例:

    public static class InfoExtension
    {
        public static void TestOne(this string s)
        {
            Console.WriteLine(s);
        }
        public static string TestTwo(this string s)
        {
            return s;
        }
    }
            //调用
            string x = "信息输出";
            Console.WriteLine(x.TestTwo());

猜你喜欢

转载自blog.csdn.net/qq_31975127/article/details/85595234