I am trying to create a method in an interface with a generic return type but I fail to cast the generic to a specific type/class. But if I put the generic on the interface instead of the method I am able to do the casting.
In other words, why does this work
public class Rain {
public string propA {get;set;}
}
public interface IFoo<T> {
T foo();
}
public class Bar : IFoo<Rain> {
Rain foo() {
//...
return new Rain();
}
}
public bar = new Bar();
Rain rain = bar.foo();
But it is not possible to do this?
public class Rain {
public string propA {get;set;}
}
public interface IFoo {
T foo<T>();
}
public class Bar : IFoo {
T foo<T>() {
//...
return new Rain();
}
}
public bar = new Bar();
Rain rain = bar.foo<Rain>();
Is there any other way around ( without using Convert.ChangeType())?