隐式转换错误

问题描述:

请也建议我更改.. 不能将类型'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); 
      } 
     } 

    } 
} 
+8

编译器已经找到了你的错误。 – BoltClock 2011-03-09 04:26:36

请给我建议过的变化..

请执行下列任何一个:

  • 让你的方法接受的IEnumerable<int>代替IEnumerable<uint>

  • 使用List<uint>而不是List<int>

int表示的32位整数有符号的范围内,同时uint表示的32位整数无符号范围。由于值范围的差异,您不能交换intuint这两种通用类型的类别。

的问题是,你不能施放uintint

+0

这是完全错误的;你**可以**将'uint'转换为'int'。只有在溢出的情况下,未经检查的强制转换不会导致溢出,而​​强制转换会引发异常。 – BoltClock 2011-03-09 04:34:35

+0

好吧,我应该说他们是不同的类型 - 他们可以被投入对方,但名单和名单是不一样的。 – Marco 2011-03-09 04:36:58

+0

谢谢你everyone..im一个完整的新手和感谢对我的耐心:) – Krishna 2011-03-09 04:37:21

List<uint> itemSet = eventsSet.toList();