下面是我班的代码。该代码创建一个 ArrayList。然后它向 ArrayList 添加一些“PipesList”,在每个列表中添加管道。
我想编写一个方法 -RemoveTheSmallPipes 来摆脱所有长度小于 19 的管道。为此,我编写了一段我不知道是否有效的代码!由于代码引发错误:
编译器错误消息:CS0050:不一致的可访问性:返回类型“System.Collections.Generic.List”比方法“Program.RemoveTheSmallPipes(System.Collections.Generic.List)”更难访问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Class1
/// </summary>
public class Program
{
static void Main(string[] args)
{
List<PipesList> lstPipeTypes = new List<PipesList>();
lstPipeTypes.Add(new PipesList("PVC Pipes"));
lstPipeTypes[0].Pipes.Add(new Pipe("The blue pipe", 12));
lstPipeTypes[0].Pipes.Add(new Pipe("The red pipe", 15));
lstPipeTypes[0].Pipes.Add(new Pipe("The silver pipe", 6));
lstPipeTypes[0].Pipes.Add(new Pipe("The green pipe", 52));
lstPipeTypes.Add(new PipesList("Iron Pipes"));
lstPipeTypes[1].Pipes.Add(new Pipe("The gold pipe", 9));
lstPipeTypes[1].Pipes.Add(new Pipe("The orange pipe", 115));
lstPipeTypes[1].Pipes.Add(new Pipe("The pink pipe", 1));
lstPipeTypes.Add(new PipesList("Chrome Pipes"));
lstPipeTypes[2].Pipes.Add(new Pipe("The grey pipe", 12));
lstPipeTypes[2].Pipes.Add(new Pipe("The black pipe", 15));
lstPipeTypes[2].Pipes.Add(new Pipe("The white pipe", 19));
lstPipeTypes[2].Pipes.Add(new Pipe("The brown pipe", 60));
lstPipeTypes[2].Pipes.Add(new Pipe("The peach pipe", 16));
lstPipeTypes = RemoveTheSmallPipes(lstPipeTypes);
foreach (var pipeList in lstPipeTypes)
{
Console.WriteLine("PipesList: {0}", pipeList.pipeType);
foreach (var pipe in pipeList.Pipes)
{
Console.WriteLine("{0}, length: {1}", pipe.name, pipe.length);
}
Console.WriteLine();
}
Console.WriteLine("Done, press return to exit");
Console.ReadLine();
}
public static List<PipesList> RemoveTheSmallPipes(List<PipesList> lstPipeTypes)
{
//Place your code in here
//It should remove all pipes that have lengths lower than 19.
foreach (var pipeList in lstPipeTypes)
{
foreach (var pipe in pipeList.Pipes)
{
lstPipeTypes.RemoveAll(i => pipe.length < 19);
}
}
return lstPipeTypes;
}
}
class PipesList
{
public string pipeType;
public List<Pipe> Pipes;
public PipesList(string newBoxType)
{
pipeType = newBoxType;
Pipes = new List<Pipe>();
}
}
class Pipe
{
public string name;
public float length;
public Pipe(string newName, float newLength)
{
this.name = newName;
this.length = newLength;
}
}