2011-05-01 15 views
3

我是C#编程的新手,遇到了一个难以逾越的障碍。编译错误CS0305

我得到这个编译错误:

CS0305: Using the generic type 'System.Collections.Generic.IEnumerable' reuires 1 type arguments

与此代码;

class Program 
    { 
     static void Main(string[] args) 
     { 
      Car c = new Car(); 
      c.PetName = "Frank"; 
      c.Speed = 55; 
      c.colour = "Green"; 

      Console.WriteLine("Name = : {0}", c.PetName); 
      c.DisplayStats(); 

      Garage carLot = new Garage(); 

      // Hand over each car in the collection 
      foreach (Car c in carLot) 
      { 
       Console.WriteLine("{0} is going {1} MPH", 
        c.PetName, c.CurrentSpeed); 
      } 

      Console.ReadLine(); 
     } 

     class Car 
     { 
      //Automatic Properties 
      public string PetName { get; set; } 
      public int Speed { get; set; } 
      public string colour { get; set; } 

      public void DisplayStats() 
      { 
       Console.WriteLine("Car Name: {0}", PetName); 
       Console.WriteLine("Speed: {0}", Speed); 
       Console.WriteLine("Color: {0}", colour); 
      } 
     } 

     public class Garage 
     { 
      private Car[] CarArray = new Car[4]; 

      // Fill with some car objects on startup. 
      public Garage() 
      { 
       carArray[0] = new Car("Rusty", 30); 
       carArray[1] = new Car("Clunker", 55); 
       carArray[2] = new Car("Zippy", 30); 
       carArray[3] = new Car("Fred", 30); 
      } 
     } 

     public IEnumerator GetEnumerator() 
     { 
      foreach (Car c in carArray) 
      { 
       yield return c; 
      } 
     } 

    } 

我该如何解决这个问题?

+0

什么地方从代码丢失:一类必须实现的'IEnumerable'或IEnumerable的''接口在'foreach'语句中使用。我在Garage类中没有看到。 – 2011-05-01 03:39:42

+1

@Etienne,它更直观地实现这些接口之一,但它不是必需的。*有一个合适的'GetEnumerator'方法就足够了。如果感兴趣,请参阅C#语言规范的第8.8.4节。 – 2011-05-01 03:52:33

+0

@安东尼有趣,不知道。再次,'Garage'类中没有'GetEnumerator'方法。 – 2011-05-01 03:56:46

回答

1

你有比这更多的错误。但是,特别是对于该错误,您在foreach中循环了Garage,但该类不公开枚举器,主要是因为方法GetEnumerator实际上在类之外。移动Garage里面的方法,然后你就可以直接进入下一次崩溃的场景。

其实,对于那个错误,您需要using System.Collections;然后您需要移动GetEnumerator方法。就像我说的,你在这段代码中有很多错误。

+0

@Tony,我们是计算机程序员......我们只能改变错误信息。 – corlettk 2011-05-01 03:40:15

+0

谢谢你,帮助了很多 – TCol 2011-05-01 04:46:34

6

IEnumerable有两种变体,通用变体(System.Collections.Generic名称空间中的变体)接受一个类型变量,该变量指定了该可枚举类型包含的对象的类型。另一个(包含在System.Collections名称空间中)没有类型参数,因此公开类型object - 您似乎声明/使用非通用变体,但不使用System.Collections名称空间。

我认为快速的方法来解决您的特定编译错误是把下面的你的源代码文件的顶部:

using System.Collections; 

或者您可以改用通用版本(你应该尝试做尽可能因为它是类型安全的)通过指定类型参数在声明IEnumerable,像这样:

IEnumerable<Car> 
IEnumerator<Car> 

你也可能需要阅读An Introduction to C# Generics

您似乎还有一些错误,但似乎它们可能来自复制和粘贴源代码的问题(具体地,Garage未实现IEnumerable,通用或非通用版本,并且GetEnumeratorProgram班,而不是Garage班)。

+0

非常有帮助,谢谢 – TCol 2011-05-01 04:33:25

0

你有很多错别字。正如其他人所说的,您的具体答案是您需要为您的类Garage语句添加“:IEnumerable”。

这里是固定足够干净编译代码:

class Program 
{ 
    static void Main (string[] args) 
    { 
     Car c = new Car ("Frank", 55); 
     c.colour = "Green"; 

     Console.WriteLine ("Name = : {0}", c.PetName); 
     c.DisplayStats(); 

     Garage carLot = new Garage(); 

     // Hand over each car in the collection 
     foreach (Car ch in carLot) { 
      Console.WriteLine ("{0} is going {1} MPH", ch.PetName, ch.Speed); 
     } 

     Console.ReadLine(); 
    } 

    class Car 
    { 
     //Automatic Properties 
     public string PetName { get; set; } 
     public int Speed { get; set; } 
     public string colour { get; set; } 

     public void DisplayStats() 
     { 
      Console.WriteLine ("Car Name: {0}", PetName); 
      Console.WriteLine ("Speed: {0}", Speed); 
      Console.WriteLine ("Color: {0}", colour); 
     } 

     public Car(string petname, int speed) { PetName = petname; Speed = speed; } 
    } 

    public class Garage : IEnumerable 
    { 
     private Car[] carArray = new Car[4]; 

     // Fill with some car objects on startup. 
     public Garage() 
     { 
      carArray[0] = new Car ("Rusty", 30); 
      carArray[1] = new Car ("Clunker", 55); 
      carArray[2] = new Car ("Zippy", 30); 
      carArray[3] = new Car ("Fred", 30); 
     } 

     public IEnumerator GetEnumerator() 
     { 
      foreach (Car c in carArray) { 
       yield return c; 
      } 
     } 

    } 

} 
+1

非常感谢:-) – TCol 2011-05-01 04:26:12