Is it possible to create an extension method on a generic list with of a specific type for example
List<MyClass> myList = new List<MyClass>();
myList<MyClass>.MyExtensionMethod();
Its important that the MyExtensionMethod only works with a list of type MyClass.
I suspect that this is not possible so what options are there to achieve this?
Ofcourse it is:
public static void MyExtensionMethod(this List<MyClass> m)
It's not clear what you mean by myList<MyClass>
but this method will work for all instances of List<MyClass>
Try this :
public static class ListExtensions
{
public static void ExtensionMethodName<T>(this List<T> list) where T : MyClass
{
//Code
}
// Or
public static void ExtensionMethodName(this List<MyClass> list)
{
//Code
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With