C#:从列表中删除项目< gt; ...并且该项目不被识别为字符串?

发布于 2025-02-12 13:44:11 字数 1466 浏览 1 评论 0 原文

Newbie到C#这里。在我的代码中,我想构建一个字符串列表,然后稍后将第一个元素从列表的最前面弹出:

 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告诉我,第18行有一个语法错误,特别是此部分:

this.listo.RemoveAt(0)

错误是,没有非常有用的描述:

参数'数字'不能从键入转换为typeb

一种方法中一个参数的类型与 当班级实例化时通过。这个错误通常会出现 以及CS1502。有关如何解决此问题的讨论,请参见CS1502 错误。

cs1502

当参数类型传递给方法时,会发生此错误 不匹配该方法的参数类型。如果称为方法 被超载,然后没有一个超载版本具有签名 与正在传递的参数类型相匹配。

那怎么了?问题的根源是:

  1. 我创建一个列表,然后用字符串填充它
  2. ,我尝试从列表中删除字符串...只有编译器不再认为元素是字符串。

如果我需要在这里做一些特别的事情?为什么编译器在删除列表时不识别元素的数据类型?谢谢

newbie to C# here. 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:

this.listo.RemoveAt(0)

The error is CS1503, which doesn't have a very helpful description:

Argument 'number' cannot convert from TypeA to TypeB

The type of one argument in a method does not match the type that was
passed when the class was instantiated. This error typically appears
along with CS1502. See CS1502 for a discussion of how to resolve this
error.

CS1502 is vaguely helpful:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2025-02-19 13:44:11

在您的代码中,

listo.RemoveAt(0) 

将从列表中删除0个元素。它不会删除所有元素。

作为一个简单的测试,您可以使用此代码进行测试:

    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
    }

您可以执行此操作;

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

对于推送和流行功能,请使用堆栈。

In your code,

listo.RemoveAt(0) 

will remove the 0th element from the list. 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.

写给空气的情书 2025-02-19 13:44:11

不返回任何内容(它具有 void 返回类型)。您需要像

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

这样删除之前获取第一个元素,请考虑使用 stack&lt; t&gt; 而不是 list&lt; t&gt; as @yassinmi指出。

List<T>.RemoveAt(index) does not return anything (it has void return type). 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文