where T : IEnumerable<T> method constraint

Bart Juriewicz

From time to time I'm trying to torment the C# compiler. Today I came up with this:

static void CallFirst<T>(T a) where T : IEnumerable<T>
{
    a.First().ToString();
}

It was simple mistake, as I wanted to create the generic method that takes collection as parameter, which of course should look like this:

static void CallFirst2<T>(IEnumerable<T> a)
{
    a.First().ToString();
}

Anyway, is it even possible to call the CallFirst() method? Every time the collection is passed, the collection of collections is expected.

If it is not, shouldn't it be taken as compile time error?

Henrik

Sure:

class Test : IEnumerable<Test>
{
    IEnumerator<Test> IEnumerable<Test>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

    ...

var test = new Test();
CallFirst(test);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using IEnumerable<T> as an input to a method

From Dev

Using IEnumerable<T> as an input to a method

From Dev

Why does a Generic<T> method with a "where T : class" constraint accept an interface

From Dev

Assign List<T> to IEnumerable<T> in static method

From Dev

Use IEnumerable<Expression<Func<T, Object>>> in method

From Java

passing IEnumerable<T> to API method in Blazor WASM

From Dev

Generic Method Fails to Utilize Covariance on IEnumerable<T>

From Dev

Why is IEnumerable(of T) not accepted as extension method receiver

From Dev

Invoking Generic Method with the parameter of IEnumerable<T>

From Dev

get IEnumerable<T> in mvc post method argument

From Dev

implement ienumerable with ienumerable<T>

From Dev

Can I take either string or IEnumerable<T>, where T is string or IEnumerable<T>?

From Dev

LINQ IEnumerable<T[]> to IEnumerable<T>

From Dev

method signature pass parameter as IEnumerable<T> or as List<T>

From Dev

How to mock a method returning Task<IEnumerable<T>> with Task<List<T>>?

From Dev

method signature pass parameter as IEnumerable<T> or as List<T>

From Dev

Generic method where T implements Interface<T>

From Dev

ienumerable to ienumerable<t> without losses

From Dev

Casting an IEnumerable to IEnumerable<T> with reflection

From Dev

Extension method for IEnumerable<T> which returns type of -this-

From Dev

How to create an HtmlHelper extension method that will bind an IEnumerable<T> to a table

From Dev

IEnumerable<T> - How to use one method for both enumerators

From Dev

Get IEnumerable<T> in generic method based on specific property value

From Dev

Delegate Method parameter from IEnumerable<T> to specific type

From Dev

Select from IEnumerable<T> to IEnumerable<Quantity<T>>

From Dev

Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

From Dev

Adding IEnumerable<T> items to IEnumerable<T>

From Dev

Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

From Dev

cannot convert from IEnumerable<T> to IEnumerable<T>

Related Related

  1. 1

    Using IEnumerable<T> as an input to a method

  2. 2

    Using IEnumerable<T> as an input to a method

  3. 3

    Why does a Generic<T> method with a "where T : class" constraint accept an interface

  4. 4

    Assign List<T> to IEnumerable<T> in static method

  5. 5

    Use IEnumerable<Expression<Func<T, Object>>> in method

  6. 6

    passing IEnumerable<T> to API method in Blazor WASM

  7. 7

    Generic Method Fails to Utilize Covariance on IEnumerable<T>

  8. 8

    Why is IEnumerable(of T) not accepted as extension method receiver

  9. 9

    Invoking Generic Method with the parameter of IEnumerable<T>

  10. 10

    get IEnumerable<T> in mvc post method argument

  11. 11

    implement ienumerable with ienumerable<T>

  12. 12

    Can I take either string or IEnumerable<T>, where T is string or IEnumerable<T>?

  13. 13

    LINQ IEnumerable<T[]> to IEnumerable<T>

  14. 14

    method signature pass parameter as IEnumerable<T> or as List<T>

  15. 15

    How to mock a method returning Task<IEnumerable<T>> with Task<List<T>>?

  16. 16

    method signature pass parameter as IEnumerable<T> or as List<T>

  17. 17

    Generic method where T implements Interface<T>

  18. 18

    ienumerable to ienumerable<t> without losses

  19. 19

    Casting an IEnumerable to IEnumerable<T> with reflection

  20. 20

    Extension method for IEnumerable<T> which returns type of -this-

  21. 21

    How to create an HtmlHelper extension method that will bind an IEnumerable<T> to a table

  22. 22

    IEnumerable<T> - How to use one method for both enumerators

  23. 23

    Get IEnumerable<T> in generic method based on specific property value

  24. 24

    Delegate Method parameter from IEnumerable<T> to specific type

  25. 25

    Select from IEnumerable<T> to IEnumerable<Quantity<T>>

  26. 26

    Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

  27. 27

    Adding IEnumerable<T> items to IEnumerable<T>

  28. 28

    Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

  29. 29

    cannot convert from IEnumerable<T> to IEnumerable<T>

HotTag

Archive