Why does this code not compile, although interfaces are reference types?

.net, c#

Solution

Because it's about the real type of the object encapsulated inside interface. By declaring just `ISomeInterface` , you do not define the must condition: T has to be a reference type.

Because I can have:

public interface IStructInterface {
}
public struct A : IStructInterface {
}

and this is a value type.

By defining additional constrain class, you declare that this is reference type.

Problem

I am confused. In Why are interfaces in .Net reference types? it is said that Interfaces in .Net are reference types. The first code snippet does not compile. It says something like "T must be a reference type..." ``` public ISomeInterface DoMagic<T>(Expression<Func<object>> action, Tuple<string, DateTime, decimal> tuple) where T : ISomeInterface { Magician m = new Magician(); return m.Magic<T>(()=> action, tuple.Item3); } ``` The second compiles. ``` public ISomeInterface DoMagic<T>(Expression<Func<object>> action, Tuple<string, DateTime, decimal> tuple) where T : class, ISomeInterface { Magician m = new Magician(); return m.Magic<T>(()=> action, tuple.Item3); } ``` If Interfaces are reference types why does the first code snippet not compile?

Original source

Related problems