Extension methods versus extension methods - CSharp Custom Type

CSharp examples for Custom Type:Extension Methods

Introduction

If two extension methods have the same signature, the extension method must be called as an ordinary static method to disambiguate the method to call.

If one extension method has more specific arguments, however, the more specific method takes precedence.

Classes and structs are considered more specific than interfaces.

The following code calls StringHelper's IsCapitalized method:

Demo Code

static class StringHelper
{
    public static bool IsCapitalized(this string s) { return false; }
}
static class ObjectHelper
{
    public static bool IsCapitalized(this object s) { return false; }
}
class Test/*ww  w . j a va  2  s .c o m*/
{
    static void Main()
    {
        bool test1 = "Test".IsCapitalized();
    }
}

Related Tutorials