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方法

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2022-02-18
  • 2021-10-15
  • 2022-12-23
  • 2021-09-01
猜你喜欢
  • 2022-12-23
  • 2021-08-03
  • 2021-06-06
  • 2021-12-01
  • 2022-12-23
  • 2022-01-31
相关资源
相似解决方案