2011-05-01 51 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