sealed class 做函数扩展的方法,dotnet 3.0

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

namespace ClassLibrary
{
    public sealed class SealedClass
    {

    }
    public static class Extension
    {
       
        public const string sex="male";
        public static string Foo(this SealedClass s, string a)
        {
            return "zbo";
        }
    }
}


作用,对不能改的代码做扩展,比方说第三方的代码,系统的String类,等等。


绑定的顺序,先找有没有实例的实现,没有的话才回去找扩展的实现。

msdn:You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself. In other words, if a type has a method named Process(int i), and you have an extension method with the same signature, the compiler will always bind to the instance method. When the compiler encounters a method invocation, it first looks for a match in the type's instance methods. If no match is found, it will search for any extension methods that are defined for the type, and bind to the first extension method that it finds. 


http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值