<out T>和之间有什么区别<T>?例如:
public interface IExample<out T>
{
...
}
Run Code Online (Sandbox Code Playgroud)
与
public interface IExample<T>
{
...
}
Run Code Online (Sandbox Code Playgroud) 我有以下类和接口:
public interface IThing
{
string Name { get; }
}
public class Thing : IThing
{
public string Name { get; set; }
}
public abstract class ThingConsumer<T> where T : IThing
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个工厂将返回从ThingConsumer派生的对象,如:
public class MyThingConsumer : ThingConsumer<Thing>
{
}
Run Code Online (Sandbox Code Playgroud)
我的工厂目前看起来像这样:
public static class ThingConsumerFactory<T> where T : IThing
{
public static ThingConsumer<T> GetThingConsumer(){
if (typeof(T) == typeof(Thing))
{
return new MyThingConsumer();
}
else
{
return null;
}
}
} …Run Code Online (Sandbox Code Playgroud)