c# - 带有接口参数的 C# 泛型类
问题描述
我尝试创建相同的基本泛型变量并将每个具有继承相同接口的参数的泛型对象分配给它,但我收到错误“无法隐式转换类型”。有人可以帮助我理解这一点吗?
public interface ISomthink
{
}
public class Somethink: ISomthink
{
}
public class Somethink2: ISomthink
{
}
public class MyGeneric<T> where T:ISomthink
{
}
public class Program
{
public static void Main(string[] args)
{
ISomthink s1 = null;
s1=new Somethink();
s1 = new Somethink2();
MyGeneric<ISomthink> sg = null;
sg=new MyGeneric<Somethink>();//Error Cannot implicitly convert type
sg = new MyGeneric<Somethink2>();//Error Cannot implicitly convert type
}
}
解决方案
推荐阅读
- java - 在实时数据库“firebase”中处理错误情况的方法
- python - 如何使用数据框中的散点图数据制作热图?
- javascript - 如果 SQL 中有数据则显示按钮,否则隐藏
- javascript - 将项目推送到“for of”内的数组未按预期工作
- html - 网站上的链接标签在点击时四处移动?
- javascript - 如何使用 Jest/Puppeteer 等到元素从 DOM 中移除
- java - Java 8:同时编写排序、过滤和计数的函数式方法
- c# - 将 TResult 转换为 Func
在 Xamarin.forms 中 - laravel - 如何让单元测试忽略 Laravel 中的中间件
- typescript - 如何从接口的属性中提取“枚举类型”?