Linq原理(扩展方法)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    static class Book
    {
        public static Double ToDouble(this string s)
        {
            return Double.Parse(s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("12".ToDouble());
            Console.Read();
        }
    }
}

扩展方法一定要放在静态类中,扩展方法必须是静态的,注意参数(this string s)  this 后的类型表示扩展那个类型

上面代码的意思是扩展string 中的ToDouble方法

原文地址:https://www.cnblogs.com/blosaa/p/2055869.html