c# - 为什么隐式转换在 C# 中不起作用
问题描述
请指出Error1和Error2的原因。
Error1是使用接口作为 IList 类型时不执行隐式转换的错误。
Error2是 int 类型在使用带有 IComparable 参数的函数时不执行隐式转换的错误。
class Program
{
class Test : IComparable
{
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
}
static void Main(string[] args)
{
var testList = new List<Test>();
var testArr = new Test[]{ };
FunctionList(testList);//***Error1***
FunctionArr(testArr);
var intArr = new int[] {};
FunctionArr(intArr);//***Error2***
}
static void FunctionList(IList<IComparable> comparables)
{
//..
}
static void FunctionArr(IComparable[] comparables)
{
//..
}
}
解决方案
推荐阅读
- javascript - D3 Zoom/Pan 在具有许多数据点的 React 应用程序中性能缓慢
- python-3.x - GUI调用的调试函数
- python - PyCharm 导入“contractions”包时出现错误“No module name 'ahocorasick'”
- python - 如何根据行在数据框中设置值?
- flutter - 有条件地使用 futurebuilder : Flutter
- java - 此文件不属于该项目:IntelliJ IDEA
- telegram-bot - InlineKeyboardMarkup 类型的对象不是 JSON 可序列化的
- java - 我需要在 Java 上创建一个多重集
- r - 如何为缺失数据插入新行,其间隔可能在 R 中变化几分钟
- python - Django 模型字符串表示