根据条件从列表中删除项目

时间:2012-06-11 11:45:04

标签: c# c#-4.0 c#-3.0

下面是我班级的代码。代码创建一个ArrayList。然后它向ArrayList添加了许多“PipesList”,在每个列表中添加了Pipes。

我想写一个方法-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;
    }
}

4 个答案:

答案 0 :(得分:4)

您的PipesList课程为internal,因此只有同一个程序集中的其他代码才能看到它。您的RemoveTheSmallPipes方法公开,但在其签名中引用PipesList。这是不允许的。

RemoveTheSmallPipes方法设为内部,或将PipesList公开。

顺便说一下,你的实现也有些奇怪。目前还不清楚为什么你有两个级别的循环一个RemoveAll调用,但事实上你不在身体内使用你的lambda表达式参数(i) lambda表达式非常可疑。目前还不清楚你要做什么,但我认为你的代码目前没有达到预期目的......

编辑:根据您的描述,我怀疑身体应该是这样的:

foreach (var pipeList in lstPipeTypes)
{
    pipeList.Pipes.RemoveAll(pipe => pipe.length < 19);
}

答案 1 :(得分:0)

您的Pipe和PipesList类不是公共的,但是由公共方法返回。

答案 2 :(得分:0)

Class PipesList是内部的(默认情况下),但RemoveTheSmallPipes()是公共的。

将PipesList类设为public,或者将RemoveTheSmallPipes()设为内部。

答案 3 :(得分:0)

  1. 编译器显示以下错误:

    “不一致的可访问性:返回类型System.Collections.Generic.List比方法Program.RemoveTheSmallPipes(System.Collections.Generic.List)更难访问”“

    这是指到各个类型的可见性:您的返回类型List<PipesList>内部(因为通用参数PipesList内部),但您的方法是 public (即比内部更明显)。使您的方法内部,然后此编译器错误消息应该消失。

  2. 您的RemoveTheSmallPipes方法可能会抛出异常;迭代时不应该更改集合。
相关问题