简体   繁体   中英

How do extension methods work in C#?

I'm not entirely sure as to how to figure out if a method is an extension method or not. I read the following guidelines:

  1. The method has to be in a static class (for example "IntExtensions").

  2. The method needs to be "public static" (for example public static int Half(this int source) )

My issues with the extension methods are:

  1. I don't understand is how we know which class the method is extending. Is it the class that has this preceding it? So in the example above it would be string?
  2. What role does the static class that encloses the method play? The whole syntax seems very confusing.
  3. couldn't I group a number of different extension methods for different classes under the same class ( which doesn't seem to make sense since I want to extend only 1 class ) For example:

/

public static class IntExtensions 
{
    // method has to be static, first parameter is tagged with the 'this'
    // keyword, the type of the parameter is the type that is extended.
    public static int Half(this int source) 
    {
        return source / 2;
    }

    public static string foo( this string s)
    {
        //some code
    }
}
  1. The type the extension method is extending is directly after the this keyword in the signature since this is the instance of the object that your method will execute on.

  2. Extension methods must be static since there is no instance of the class to call them on (they are passed the instance instead). Using a static class ensures that all methods are static as well.

  3. Yes, you could. As you said, though, that doesn't make a whole lot of sense. Consumers of the class would then be exposed to all of the extension methods for all types rather than for the methods related to a single type (which is why I usually organize my extension method classes by the type that they operate on).

I don't understand is how we know which class the method is extending. Is it the class that has this preceding it?

Yes. It's always the first parameter of the method. You can't decorate any other parameter with this . (And it's the fact that this is present which tells the compiler that you're trying to write an extension method.)

What role does the static class that encloses the method play? The whole syntax seems very confusing.

It's just a class to "house" the extension method. Frequently extension methods don't use any other members of the class in which they're written, although they can. (For example, an extension method can use a private static field within the static class enclosing it.)

In many ways - including this one - an extension method is just a normal static method. It has to be in a top-level static non-generic class, and the first parameter can't be out or ref , but once those conditions are satisfied, it's just a normal method as far as the method implementation is concerned. It's "special" as far as callers are concerned, in terms of how you can invoke the method. (It can be invoked in the same way as any other static method as well, mind you.)

couldn't I group a number of different extension methods for different classes under the same class ( which doesn't seem to make sense since I want to extend only 1 class )

Yes, you can. It's not usually a good idea, but you can absolutely do that.

which class the method is extending. Is it the class that has this preceding it? So in the example above it would be string ?

Yes.

What role does the static class that encloses the method play? The whole syntax seems very confusing.

See Extension Methods (C# Programming Guide) :

In your code you invoke the extension method with instance method syntax. However, the intermediate language (IL) generated by the compiler translates your code into a call on the static method.

So the class has to be static, because the compiler won't create an instance of the class.

couldn't I group a number of different extension methods for different classes under the same class

Yes, you can.

1) Yes, it's the type after the 'this'

2) A method must always be within a class. It can't be within a non-static class, or it would be an member, and require an object of that class's type to operate. So the only option is a static class, which can be called from anywhere.

3) Yes, you can group them however you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM