3

Can an extension method have a say: List as one of the parameters?

public static IEnumerable<XElement> GetSequenceDescendants(this IEnumerable<XElement> elements, params List<XName> names)
        {
            //do something
        }

Is there any restriction on the types of parameters an extension method can have?

GilliVilla
  • 4,998
  • 11
  • 55
  • 96
  • 5
    Most extension methods have generic parameters. – Gabe Jan 24 '11 at 21:49
  • 1
    If you take out params from your extension method declaration, your code can compile. If you don't want to take out params from your extension method, you need to change the List to XName[] – Harvey Kwok Jan 24 '11 at 21:55
  • 1
    You are asking two questions realy, for the first one List cannot be param by itself, you need to define it as array like "params XName[] names", unless you make the param array of Lists. – Kris Ivanov Jan 24 '11 at 21:56

2 Answers2

7

The short answer is that an extension method is just a public static method which can be accessed like an instance method of the first parameter (thanks to the this keyword). This means you can use the same parameters you could use in any static method.

But if you want the parameters to actually be generic you'd need to change your method to this:

public static IEnumerable<TElement> GetSequenceDescendants<TElement, TName>(this IEnumerable<TElement> elements, List<TName> names)
{
    //do something
}

You have to specify all the generic arguments in your method definition.

Also, you can't use the params keyword with anything but an array, i.e. params TName[] is okay, but params List<TName> isn't.

Bennor McCarthy
  • 11,415
  • 1
  • 49
  • 51
  • 1
    `` does not need to be part of the method signature. These aren't type parameters to the method. If it were `IEnumerable` and `List`, that would be a different story. – Anthony Pegram Jan 24 '11 at 21:54
  • Not entirely correct: you **can** use `T[] params` in extensions methods. See the top answer (`In()`) in the question to which I reference in my answer. –  Jan 24 '11 at 22:00
  • I didn't say you can't use params in an extension method. I said you can only use it with an array. i.e. params T[] is okay, but params List isn't. I've clarified it in my answer in case anyone else misinterprets it. – Bennor McCarthy Jan 24 '11 at 22:02
  • Note that with your edit you have made the method itself generic, which I don't *believe* was the question (although it could have been, and it's not necessarily a bad idea). If the OP wants the method to deal *specifically* with the `XElement` and `XName` classes, then this would need to be altered back to a modified version of his original signature. – Anthony Pegram Jan 24 '11 at 22:05
  • @Anthony Pegram: Agreed. It was quite hard to tell exactly what was being asked. – Bennor McCarthy Jan 24 '11 at 22:06
0

There are limits on type inferance (as in when the compiler can guess the generic types from the parameters. The extension methods have one limitation less: you can "dereference" null pointers (you can write and extension method bool IsNullorEmpty(this string) and use it on a null string.

For examples: Linq is full of nice generic extension methods, and you can see a lot of great examples (with smart use of generics) in this question:

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

Community
  • 1
  • 1