C# 编译错误CS0305
我是C#编程新手,遇到了一个无法逾越的障碍 我得到了这个编译错误: CS0305:使用泛型类型“System.Collections.generic.IEnumerable”需要1个类型参数 使用此代码C# 编译错误CS0305,c#,C#,我是C#编程新手,遇到了一个无法逾越的障碍 我得到了这个编译错误: CS0305:使用泛型类型“System.Collections.generic.IEnumerable”需要1个类型参数 使用此代码 class Program { static void Main(string[] args) { Car c = new Car(); c.PetName = "Frank"; c.
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;
}
}
}
如何解决此问题?您的错误不止这些。但特别是对于该错误,您在
foreach
中循环Garage
,但该类不公开枚举器,主要是因为方法GetEnumerator
实际上在该类之外。将该方法移动到车库内
,然后您就可以一直到达下一次碰撞的现场
实际上,对于这个错误,您需要使用System.Collections
然后需要移动GetEnumerator
方法。正如我所说,这段代码中有大量错误。IEnumerable有两种变体,泛型变体(位于System.Collections.generic
命名空间中)接受指定可枚举对象类型的类型参数。另一个(包含在System.Collections
命名空间中)没有类型参数,因此公开了类型对象
——您似乎在声明/使用非泛型变量,但没有使用System.Collections
命名空间
我认为修复特定编译错误的快速方法是将以下内容放在源代码文件的顶部:
using System.Collections;
或者,您也可以在声明IEnumerable
时通过指定类型参数来使用泛型版本(您应该尽可能做到这一点,因为它是类型安全的),如下所示:
IEnumerable<Car>
IEnumerator<Car>
IEnumerable
迭代器
你可能还想读书
您似乎还有一些错误,但这些错误可能是由于复制和粘贴源代码时出现的问题(特别是
Garage
不实现IEnumerable
,通用或非通用版本,并且GetEnumerator
在程序
类上,而不是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;
}
}
}
}
代码中缺少了一些东西:类必须实现
IEnumerable
或IEnumerable
接口才能在foreach
语句中使用。我在Garage类中看不到这一点。@Etienne,实现其中一个接口更直观,但不是必需的。拥有合适的GetEnumerator代码>方法就足够了。如果感兴趣,请参阅C语言规范第8.8.4节。@Anthony很有意思,他不知道这一点。Garage
类中没有GetEnumerator
方法。@Tony,我们是计算机程序员……我们的存在只是为了更改错误消息。谢谢你的帮助