【问题标题】:C# List<String> Print ItemsC# List<String> 打印项目
【发布时间】:2019-05-28 16:29:19
【问题描述】:

在此处和 PluralSight 上的以下教程对 c# 非常陌生。

我有以下文件(我认为是类?)

程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Customers
{
    class Program
    {
        static void Main(string[] args)
        {
            CustomerList customers = new CustomerList();

            customers.AddCustomer("Apple");
            customers.AddCustomer("Microsoft");
        }
    }
}

还有 CustomerList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Customers
{
    public class CustomerList
    {
        public CustomerList()
        {
            customers = new List<string>();
        }

        public void AddCustomer(string customer)
        {
            customers.Add(customer);
        }

        private List<String> customers;
    }
}

我遇到的问题是我只想列出列表中的项目,这样做让我很难过。我确实尝试使用如下的 foreach 语句。

      foreach (string customer in customers)
        {
            Console.WriteLine(customer);
        }

来自 Program.cs 但我无法按照它的说明这样做

foreach 语句无法对'CustomerList' 类型的变量进行操作,因为'CustomerList' 不包含'GetEnumerator' 的公共实例定义

但是,如果我将所有代码保存在 Program.cs 中并使用以下代码,我就可以写出列表中的每个项目。

private List<string> customers;

static void Main(string[] args)
{
    customers = new List<String>();
    AddCustomers(customers);

    foreach(string customer in customers) {

         Console.WriteLine(customer);
    }


}

private void AddCustomers(List<string> customers)
    {
        customers.Add("Apple");
        customers.Add("Microsoft");
    }

就像我说的,很新,所以请放轻松。

【问题讨论】:

  • 首先,您为什么还要开设CustomerList 课程?只需改用List&lt;string&gt;
  • 问题是什么?
  • 您的 CustomerList 类中的“客户”列表无法从该类外部访问。

标签: c# list


【解决方案1】:

尝试在您的CustomerList 类中实现IEnumerable 接口。通过这样做,您必须添加方法GetEnumerator(),这将允许您遍历字符串列表。实现如下接口(注意类定义中的: IEnumerable):

public class CustomerList : IEnumerable 
{
    ... // Your current code \\ ...

    public IEnumerator GetEnumerator()
    {
        return customers.GetEnumerator();
    }
}

【讨论】:

  • 但在我看来,这不是我们想要的行为。
  • @bolkay 我不同意。根据他的解释和他尝试的示例代码 - 我相信这是期望的行为。它通过在 foreach 中调用他的对象来为他提供他正在寻找的输出。它可能不是最好的 解决方案,也绝对不是唯一的解决方案。但我确实相信它是 a 提供所需行为的解决方案。
  • 就做吧..公共列表客户..不需要实现IEnumerable..这里列表已经实现了
  • @dvo 我完全同意你的看法。我相信保持简单和最少的代码更改。
【解决方案2】:

您需要首先检查您的 CustomList 定义和实例 customers。实例客户不是一个集合,因此不能像错误描述所说的那样枚举。相反,它是 CustomerList 的一个实例,它有一个名为 customer 的属性。

属性 customer 是字符串的集合,可以枚举,但是,在您的情况下,您已将其声明为私有。

因此,您需要做的第一个更改是将其设为公共财产。请注意,我没有将变量设为公开,而是将其设为公共属性。您可以了解更多关于属性here

public List<String> customers {get;set;}

然后,在您的 foreach 中,您可以执行以下操作。

foreach (string customer in customers.customers)
{
    Console.WriteLine(customer);
}

但是,这可能需要进行更改才能使您的现有代码正常工作。但是,如果你要重写整个代码,你有几个选择。

a) 可以取消 CustomerList 类并直接使用 List。

var customers = new List<string>();
customers.Add("Apple");

b) 使用 CustomerList 类,但使用公共属性 Customers 直接添加到列表中

public class CustomerList
{
    public CustomerList()
    {
        customers = new List<string>();
    }

    public List<String> Customers;
}

客户代码

CustomerList customers = new CustomerList();

customers.Customers.Add("Apple");
customers.Customers.Add("Microsoft");

c) 第三种选择是实现 IEnumerable 接口,但是,考虑到您的代码要求,以及您仍在学习 C# 的事实,我会说这可能是矫枉过正。最好先对集合和类/属性有一个深刻的了解,然后再实际执行 IEnumerable 实现

【讨论】:

  • 所以在我看来,最好将 List 客户公开。而不是尝试了解 IEnumerable ?我猜想公开这份名单不是最佳做法,但在这个阶段有关系吗?我这样做会不会惹上麻烦。
  • 我建议不要将成员变量公开,而是将其设为属性。拥有公共财产完全没问题。
猜你喜欢
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 2022-12-10
相关资源
最近更新 更多