Can extension methods have common parameters?

Is it possible to use the extension method: 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 that the extension method may have?

+3
source share
2 answers

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

But if you want the parameters to actually be shared, you will need to change your method to this:

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

.

, params , , .. params TName[] , params List<TName> .

+7

(, . : "" ( bool IsNullorEmpty(this string) .

: Linq , ( ) :

#? (Codeplex.com/extensionoverflow)

0

Source: https://habr.com/ru/post/1787566/


All Articles