【问题标题】:C#: Multi element list? (Like a list of records): How best to do it?C#:多元素列表? (如记录列表):如何做到最好?
【发布时间】:2021-05-12 23:54:26
【问题描述】:

我喜欢列表,因为它们非常易于使用和管理。但是,我需要一个包含多个元素的列表,例如记录。

我是 C# 新手,感谢所有帮助! (Stackoverflow 摇滚!)

考虑一下这个简单、有效的单元素列表示例,它的效果很好:

public static List<string> GetCities()
{
  List<string> cities = new List<string>();
  cities.Add("Istanbul");
  cities.Add("Athens");
  cities.Add("Sofia");
  return cities;
}

如果我希望列表的每条记录都有两个属性,我该怎么做? (作为一个数组?)

例如这个伪代码的真实代码是什么?:

public static List<string[2]> GetCities()
{
  List<string> cities = new List<string>();
  cities.Name,Country.Add("Istanbul","Turkey");
  cities.Name,Country.Add("Athens","Greece");
  cities.Name,Country.Add("Sofia","Bulgaria");
  return cities;
}

谢谢!

【问题讨论】:

    标签: c#


    【解决方案1】:

    List&lt;T&gt; 可以保存任何类型的实例 - 因此您只需创建一个自定义类来保存您想要的所有属性:

    public class City
    {
       public string Name {get;set;}
       public string Country {get;set;}
    }
    
    ...
    
    public List<City> GetCities()
    {
       List<City> cities = new List<City>();
       cities.Add(new City() { Name = "Istanbul", Country = "Turkey" });
       return cities;
    }
    

    【讨论】:

    • Jonesome 是 C# 的新手,所以我想提一下,这个(正确的)答案也使用了 Auto-implemented Properties,非常方便。编辑:虽然他想要的似乎是一个 Country 类,它有一个 Name 和一个 List&lt;string&gt; Cities (或类似的)
    • 谢谢!这很好用!克里斯,谢谢你的小费。我阅读了您所指的参考资料,并且(基本上)明白了。
    【解决方案2】:

    为什么没有人提到Dictionary&lt;&gt;

    我认为使用Dictionary 会更容易。据我了解,OP 希望有 2 个彼此相关的值,在这种情况下,是一个国家及其首都。

    Dictionary<string, string> capitals = new Dictionary<string, string>()
    {
        {"Istanbul","Turkey"},
        {"Athens","Greece"},
        {"Sofia","Bulgaria"},
    };
    

    将“Key,Values”添加到您的字典中:

    capitals.add("Lisbon, Portugal");
    

    Other possible ways of adding to dictionary

    遍历字典中的项目:

    foreach(KeyValuePair<string, string> entry in capitals)
    {
        // do something with entry.Value or entry.Key
    }
    

    What is the best way to iterate over a Dictionary in C#?

    编辑条目:

    capital[Lisbon]= "..."
    

    即使创建自己的类(或我猜的结构)可行,但如果目标是创建一个包含 2 个彼此相关的值的列表,那么 Dictionary 会更简单。

    【讨论】:

      【解决方案3】:

      对于动态处理,您可以使用元组(在 .NET 4.0 中):

      List<Tuple<string,string>> myShinyList = new List<Tuple<string,string>> {
          Tuple.Create("Istanbul","Turkey"),
          Tuple.Create("Athens","Greece"),
          Tuple.Create("Sofia","Bulgaria")
      }
      

      【讨论】:

        【解决方案4】:
        public class City
        {
            public City(string name, string country)
            {
                Name = name;
                Country = country;
            }
        
            public string Name { get; private set; }
            public string Country { get; private set; }
        }
        
        public List<City> GetCities()
        {
            return new List<City>{
                new City("Istanbul", "Turkey"),
                new City("Athens", "Greece"),
                new City("Sofia", "Bulgaria")
            };
        }
        

        如果您真的不需要列表,而且您不太可能这样做,您可以将返回类型设为IEnumerable&lt;City&gt;,这样更通用。你仍然可以返回一个列表,但你也可以这样做:

        public IEnumerable<City> GetCities()
        {
            yield return new City("Istanbul", "Turkey"),
            yield return new City("Athens", "Greece"),
            yield return new City("Sofia", "Bulgaria")
        }
        

        如果那时您要遍历城市,直到找到土耳其的第一个城市,或者第一个以字母 I 开头的城市,您就不必实例化 所有 个城市,就像使用列表一样。相反,第一个 City 将被实例化和评估,并且只有在需要进一步评估时才会实例化后续的 City 对象。

        【讨论】:

        • 你的回答比较全!干得好!
        【解决方案5】:
        List<City<string, string, int>> cityList = new List<City<string,string, int>> {
            City.Create("Paris","France", 12000000); // City, Country, population
        }
        
        foreach(var v in City)
        {
            list.Add([0,0,1])
            Console.Write("First entry: {0}", list[0]);
        }
        

        【讨论】:

        • 你能解释一下吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-12
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 2022-01-22
        相关资源
        最近更新 更多