隐式转换错误
问题描述:
请也建议我更改.. 不能将类型'System.Collections.Generic.List'隐式转换为System.Collections.Generic.IEnumerable'。一个显式转换存在(是否缺少强制转换?)隐式转换错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
List<int> eventIDs = new List<int>() { 53,90,344,2223,2225,4497,5512};
MatchNumbers(eventIDs,2200,2300);
}
public static void MatchNumbers(IEnumerable<uint> eventsSet, int lowerBound, int upperBound)
{
if (upperBound < lowerBound)
throw new Exception("Lower bound cant be bigger");
List<int> itemSet = (List<int>)eventsSet;
for (int i = lowerBound; i <= upperBound; i++)
{
int result = itemSet.BinarySearch(i);
if (result >= 0)
Console.WriteLine("Found{0}", i);
}
}
}
}
答
请给我建议过的变化..
请执行下列任何一个:
让你的方法接受的
IEnumerable<int>
代替IEnumerable<uint>
。使用
List<uint>
而不是List<int>
。
int
表示的32位整数有符号的范围内,同时uint
表示的32位整数无符号范围。由于值范围的差异,您不能交换int
和uint
这两种通用类型的类别。
答
List<uint> itemSet = eventsSet.toList();
编译器已经找到了你的错误。 – BoltClock 2011-03-09 04:26:36