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

Amir Popovich

I have this interface:

public interface ITestInterface
{
    int TestInt { get; set; }
}

and this generic method (with a T : class constraint):

public void Test<T>() where T : class
{
    // DoSomething
}

and this call:

Test<ITestInterface>();

and everything compiles and runs while an interface is not a class (or is it?).

Why does this happen?

I first saw this on my WCF proxy class:

public partial class TestServiceClient:
     System.ServiceModel.ClientBase<TestNamespace.ITestService>, TestNamespace.ITestService

where ClientBase<T> has this definition:

public abstract class ClientBase<TChannel> : 
     ICommunicationObject, IDisposable where TChannel : class
Jakub Lortz

The class constraint means that the type must be a reference type, not necessarily a class.

From C# language specification:

The reference type constraint specifies that a type argument used for the type parameter must be a reference type. All class types, interface types, delegate types, array types, and type parameters known to be a reference type (as defined below) satisfy this constraint.

Basically, it means that the type cannot be a value type.

Value types can implement interfaces too, but casting a value type to an interface causes the value to be boxed

IComparable i = 0;

Now i stores a reference to a boxed 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

interface<T> where T : class

From Dev

Generic class where T : Class clarification

From Dev

Generics where T is class implementing interface

From Dev

Why is "where T : class" required in this example?

From Dev

Why does generic interface constraint mess up the use of extension methods?

From Dev

Why does generic interface constraint mess up the use of extension methods?

From Dev

C# generic, where T class : (inheritance class)

From Dev

Why doesn't this generic method accept any type?

From Dev

What does this mean: ClassName<T> where T : class

From Dev

Why does this program involving a generic interface method compile?

From Dev

Difference between Generic method with interface constraint and method with interface parameters

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

Why does functional interface with void return-type method accept any return-type method?

From Dev

Why HashMap with generic declaration "<? super ArrayList> does not accept value "new Object()" in the put method?

From Dev

Why does List use an explicit interface method implementation to implement non-generic interface methods?

From Dev

Why does List use an explicit interface method implementation to implement non-generic interface methods?

From Dev

Generic interface constraint

From Dev

C# - Interface generic method constraint to match derived type

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

Why generic ICollection<T> does not inherit some non-generic interface with Count property?

From Dev

Why specifying a generic argument as interface is not an error with a class constraint?

From Dev

Generic method where T implements Interface<T>

From Dev

Calling a Generic Interface Method does not work

From Java

Why does the compiler choose this generic method with a class type parameter when invoked with an unrelated interface type?

From Dev

Why a generic method of an interface can be implemented as non-generic in Java?

From Dev

Why a generic method of an interface can be implemented as non-generic in Java?

From Dev

Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

From Dev

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

From Dev

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

Related Related

  1. 1

    interface<T> where T : class

  2. 2

    Generic class where T : Class clarification

  3. 3

    Generics where T is class implementing interface

  4. 4

    Why is "where T : class" required in this example?

  5. 5

    Why does generic interface constraint mess up the use of extension methods?

  6. 6

    Why does generic interface constraint mess up the use of extension methods?

  7. 7

    C# generic, where T class : (inheritance class)

  8. 8

    Why doesn't this generic method accept any type?

  9. 9

    What does this mean: ClassName<T> where T : class

  10. 10

    Why does this program involving a generic interface method compile?

  11. 11

    Difference between Generic method with interface constraint and method with interface parameters

  12. 12

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  13. 13

    Why does functional interface with void return-type method accept any return-type method?

  14. 14

    Why HashMap with generic declaration "<? super ArrayList> does not accept value "new Object()" in the put method?

  15. 15

    Why does List use an explicit interface method implementation to implement non-generic interface methods?

  16. 16

    Why does List use an explicit interface method implementation to implement non-generic interface methods?

  17. 17

    Generic interface constraint

  18. 18

    C# - Interface generic method constraint to match derived type

  19. 19

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

  20. 20

    Why generic ICollection<T> does not inherit some non-generic interface with Count property?

  21. 21

    Why specifying a generic argument as interface is not an error with a class constraint?

  22. 22

    Generic method where T implements Interface<T>

  23. 23

    Calling a Generic Interface Method does not work

  24. 24

    Why does the compiler choose this generic method with a class type parameter when invoked with an unrelated interface type?

  25. 25

    Why a generic method of an interface can be implemented as non-generic in Java?

  26. 26

    Why a generic method of an interface can be implemented as non-generic in Java?

  27. 27

    Why generic constraint T extends Comparable<T> is not sufficient to avoid a cast?

  28. 28

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

  29. 29

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

HotTag

Archive