is it possible to write an extension method only for List<T> where T is a class that inherits from class K

Stralos

Lets say I have a Base Class:

public class Base{

}

I have a class Derived that inherits from Base Class:

public class Derived: Base{

}

Is it possible to write an extension method, for a List<T> of classes, that inherit only from the Base class, something like this:

public static string F(this List<Base> baseClass){
    return "This class inherits from Base Class";
}

The extension method should work for List<Base> and List<Derived>. But it doesn't for me.

usr
public static string F<T>(this List<T> baseClass)
  where T : Base
{
    return "This class inherits from Base Class";
}

The method must be generic because List<Derived> is not a subtype of List<Base> (and not a supertype either).

If IEnumerable is enough for you and this was an interface or delegate method I think you could solve this with covariance, too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

extension method for List<T> with both constraints class and struct

From Dev

Can't create a variable in class that inherits from Atom api

From Dev

Can't write values into a global PHP array from a class method

From Dev

Extension method to check if an object is sub class of T

From Dev

Declare List<IEntity<T>> where T:class

From Dev

Declare List<IEntity<T>> where T:class

From Dev

When Class Inherits from built-in List

From Dev

How to infer method of T from Class<T>

From Dev

Calling an extension method on a member variable of type T of class<T>

From Dev

Nullable int property turns to internal field in public class which doesn't inherits from any class

From Dev

dart, given an instance of a class is it possible to get a list of all the types it inherits from in ascending order?

From Dev

How to get Class<T> from List<T>

From Dev

How to get Class<T> from List<T>

From Dev

Call a method from console menu for a class that inherits the method

From Dev

Why can't I call an extension method from a base class of the extended type‏?

From Dev

"Cannot convert argument from Class<T>* to Class<T> &&" on list insert

From Dev

Get <T> class - possible?

From Dev

default(T) return null where T inherits from DataContext

From Dev

Can't add extension method to an abstract class in C#

From Dev

one generic class create generic extension with expression<T,bool> method

From Dev

C# Add extension Append method to class array of type T

From Dev

How to write extension method that work with base class

From Dev

interface<T> where T : class

From Dev

Can't use method from another class?

From Dev

Swift 2.0: Parametrized classes don't call proper == function if it inherits from class that is Equatable

From Dev

Why can't you inherit from a not-yet-defined class which inherits from a not-yet-defined class?

From Dev

Add an 'each' like method to a Ruby class that inherits from Hash

From Dev

Reflection of class<T>.method

From Dev

How to call a static method on a base class from a generic class<T>?

Related Related

  1. 1

    extension method for List<T> with both constraints class and struct

  2. 2

    Can't create a variable in class that inherits from Atom api

  3. 3

    Can't write values into a global PHP array from a class method

  4. 4

    Extension method to check if an object is sub class of T

  5. 5

    Declare List<IEntity<T>> where T:class

  6. 6

    Declare List<IEntity<T>> where T:class

  7. 7

    When Class Inherits from built-in List

  8. 8

    How to infer method of T from Class<T>

  9. 9

    Calling an extension method on a member variable of type T of class<T>

  10. 10

    Nullable int property turns to internal field in public class which doesn't inherits from any class

  11. 11

    dart, given an instance of a class is it possible to get a list of all the types it inherits from in ascending order?

  12. 12

    How to get Class<T> from List<T>

  13. 13

    How to get Class<T> from List<T>

  14. 14

    Call a method from console menu for a class that inherits the method

  15. 15

    Why can't I call an extension method from a base class of the extended type‏?

  16. 16

    "Cannot convert argument from Class<T>* to Class<T> &&" on list insert

  17. 17

    Get <T> class - possible?

  18. 18

    default(T) return null where T inherits from DataContext

  19. 19

    Can't add extension method to an abstract class in C#

  20. 20

    one generic class create generic extension with expression<T,bool> method

  21. 21

    C# Add extension Append method to class array of type T

  22. 22

    How to write extension method that work with base class

  23. 23

    interface<T> where T : class

  24. 24

    Can't use method from another class?

  25. 25

    Swift 2.0: Parametrized classes don't call proper == function if it inherits from class that is Equatable

  26. 26

    Why can't you inherit from a not-yet-defined class which inherits from a not-yet-defined class?

  27. 27

    Add an 'each' like method to a Ruby class that inherits from Hash

  28. 28

    Reflection of class<T>.method

  29. 29

    How to call a static method on a base class from a generic class<T>?

HotTag

Archive