Here I got a small example which causes the compliler to deliver a error of type:
“Cannot implicitly convert type ‘ProvideValueOne’ to ‘ValueProviderType'”.
In my opinion it is not possible to violate the types in any way, so why is this code not accepted?
enlightenment appreciated.
public interface IProvideValue
{
int Value { get; }
}
public class ProvideValueOne : IProvideValue
{
public int Value
{
get { return 1; }
}
}
public class ProvideValueTwo : IProvideValue
{
public int Value
{
get { return 2; }
}
}
public class BaseEntity<ValueProviderType> where ValueProviderType : IProvideValue
{
public ValueProviderType Provider
{
get{
return new ProvideValueOne(); // doesnt´t compile
}
}
}
Oh but it is:
So C# is correct to flag this. Did you perchance mean the following?
Notice the added
newconstraint.