简体   繁体   English

C#:从列表中删除项目<string> ...并且该项目未被识别为字符串?</string>

[英]C#: Removing an Item from List<String>... and the item is not recognized as a String?

newbie to C# here.新手到 C# 这里。 In my code, I want to build a list of strings, then later pop the first element off the front of the list:在我的代码中,我想构建一个字符串列表,然后将第一个元素从列表的前面弹出:

 1    public class foo
 2    {
 3        public List<String> listo;
 4
 5        public foo()
 6        {
 7            this.listo = new List<String>();
 8            listo.Add("Apples");
 9            listo.Add("Oranges");
10            listo.Add("Bananas");
11            RemoveAndPrintAll();
12        }
13
14        public void RemoveAndPrintAll()
15        {
16            while(listo.Count > 0)
17            {
18                System.Console.WriteLine(this.listo.RemoveAt(0));
19            }
20        }
21    }

MS Visual Studio tells me that line 18 has a syntax error, specifically this part: MS Visual Studio 告诉我第 18 行有语法错误,特别是这部分:

this.listo.RemoveAt(0)

The error is CS1503 , which doesn't have a very helpful description:错误是CS1503 ,它没有非常有用的描述:

Argument 'number' cannot convert from TypeA to TypeB参数“number”无法从 TypeA 转换为 TypeB

The type of one argument in a method does not match the type that was passed when the class was instantiated.方法中一个参数的类型与实例化 class 时传递的类型不匹配。 This error typically appears along with CS1502.此错误通常与 CS1502 一起出现。 See CS1502 for a discussion of how to resolve this error.有关如何解决此错误的讨论,请参阅 CS1502。

CS1502 is vaguely helpful: CS1502 有点帮助:

This error occurs when the argument types being passed to the method do not match the parameter types of that method.当传递给方法的参数类型与该方法的参数类型不匹配时,会发生此错误。 If the called method is overloaded, then none of the overloaded versions has a signature that matches the argument types being passed.如果被调用的方法被重载,则没有一个重载版本具有与传递的参数类型相匹配的签名。

So what the heck?那到底是什么? The root of the problem is this:问题的根源是这样的:

  1. I create a List, then populate it with Strings我创建一个列表,然后用字符串填充它
  2. Later, I try to remove Strings from the list... only the compiler doesn't think the elements are Strings any more.后来,我尝试从列表中删除字符串......只是编译器不再认为元素是字符串。

If there something special I need to do here?如果我需要在这里做一些特别的事情? Why would the compiler not recognize the elements' data type when it comes time to remove them from the list?为什么编译器在将元素从列表中删除时无法识别元素的数据类型? Thank you谢谢

In your code,在你的代码中,

listo.RemoveAt(0) 

will remove the 0th element from the list.将从列表中删除第 0 个元素。 It won't remove all elements.它不会删除所有元素。

As a simple test, you can use this code to test:作为一个简单的测试,你可以使用这段代码来测试:

    private static void RemoveElementsFromList()
    {
        var list = new List<string>() { "abc", "def", "efg" };

        for(int i = 0; i < list.Count; i++)
        {
            list.RemoveAt(0);
        }
        var size = list.Count; // here size = 1
    }

You can do this;你可以这样做;

if(listo != null && listo.Any())
{
    listo.RemoveRange(0, listo.Count);
    var size = listo.Count;
}

For push and pop functionality, use Stack however.但是,对于推送和弹出功能,请使用 Stack。

List<T>.RemoveAt(index) does not return anything (it has void return type). List<T>.RemoveAt(index)不返回任何东西(它有void返回类型)。 You will need to get the first element before removing it like您需要先获取第一个元素,然后再将其删除

while (listo.Count > 0)
{
    Console.WriteLine(listo[0]);
    listo.RemoveAt(0);
}

That said, consider using Stack<T> instead of List<T> as @yassinmi pointed out.也就是说,考虑使用Stack<T>而不是 @yassinmi 指出的List<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM