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

Zin Kun

I want to write extension for List<MyClass> and List<int>, List<string>, and other types.

I tried to write this code:

public static int MyMethod<T>(this List<T> list) where T : struct  where T : class
{
    int result = //do something
    return result;
}
HimBromBeere

As there´s no type that is neither a class nor a struct, applying both constraints to your member is redundant. Thus you can simply omit it completely, making your method work on any T.

public static int MyMethod<T>(this List<T> list)
{
    int result = //do something
    return result;
}

Apart from this you can of course add multiple constraints to a method, e.g. this:

public void DoSomething<T>() where T: class, new() { /* ... */ }

which would make this method exist for reference-types with a parameterless constructor.

EDIT: when you have some specific types in mind, you should consider to create two methods instead, as you can´t do much for both integers and strings. So make two methods:

public static int MyMethodForInt(this List<string> list)
{
    int result = //do something
    return result;
}
public static int MyMethodForString(this List<int> list)
{
    int result = //do something
    return result;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

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

From Dev

Change state of List<T> inside extension method

From Dev

List<T> to XML string Extension Method

From Dev

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

From Dev

C# List<object> unable to use extension method of List<T>

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

Extension Method for a List

From Dev

Add extension method to List

From Dev

ForEach List<T> Extension method - Value Or Reference Type?

From Dev

How to get an extension method to appear in a Struct

From Dev

In Python, use a method both as instance and class method

From Dev

Is it possible to use Haxe type constraints with both class fields and properties?

From Dev

Stl list of struct as a class member

From Dev

Extension Method for List<InterfaceType> is not recognized

From Dev

Extension Method for List<InterfaceType> is not recognized

From Dev

c# method won't add to list in class

From Dev

Spring MVC: RequestMapping both class and method

From Dev

Python method available for both instantiated/uninstantiated class

From Dev

Constraints on type parameters - where T: class

From Dev

Performance of List<struct> vs List<class>

From Dev

Performance of List<struct> vs List<class>

From Dev

Why do both struct and class exist in C++?

From Dev

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

From Dev

Method which accepts both int[] and List<int>

From Dev

How to call static class method from a struct?

From Dev

Defining a struct::matrix object inside a class method

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Change state of List<T> inside extension method

  4. 4

    List<T> to XML string Extension Method

  5. 5

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

  6. 6

    C# List<object> unable to use extension method of List<T>

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

    Extension Method for a List

  11. 11

    Add extension method to List

  12. 12

    ForEach List<T> Extension method - Value Or Reference Type?

  13. 13

    How to get an extension method to appear in a Struct

  14. 14

    In Python, use a method both as instance and class method

  15. 15

    Is it possible to use Haxe type constraints with both class fields and properties?

  16. 16

    Stl list of struct as a class member

  17. 17

    Extension Method for List<InterfaceType> is not recognized

  18. 18

    Extension Method for List<InterfaceType> is not recognized

  19. 19

    c# method won't add to list in class

  20. 20

    Spring MVC: RequestMapping both class and method

  21. 21

    Python method available for both instantiated/uninstantiated class

  22. 22

    Constraints on type parameters - where T: class

  23. 23

    Performance of List<struct> vs List<class>

  24. 24

    Performance of List<struct> vs List<class>

  25. 25

    Why do both struct and class exist in C++?

  26. 26

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

  27. 27

    Method which accepts both int[] and List<int>

  28. 28

    How to call static class method from a struct?

  29. 29

    Defining a struct::matrix object inside a class method

HotTag

Archive