【问题标题】:List<> with numbers and textList<> 带有数字和文本
【发布时间】:2014-12-13 16:07:12
【问题描述】:

有没有类似于二维数组的List&lt;&gt;?每个条目都有一个数字和文本。

【问题讨论】:

  • 为什么不创建一个包含数字和文本的类?
  • Google > c# collections > 第一个链接哇,答案...
  • 您需要 31 分钟才能提及这一点? :D @t3chb0t
  • @dotctor: 是的,它发生了,也许是因为我没有因为立即给出答案而得到报酬,同时我还做其他事情:-]
  • @t3chb0t 干得好! Google 是朋友。

标签: c# .net list


【解决方案1】:

您可以使用Dictionary&lt;int,String&gt;

示例:

Dictionary<int,string> samp = new Dictionary<int,string>();
dictionary.Add(1, "text1");
dictionary.Add(2, "text2");

或者,有一个自定义类来定义您的要求

public class Sample
{
   public int Number;
   public string Text;
}

示例:

List<Sample> req = new List<Sample>();
Sample samObj = new Sample();
samObj.Number = 1;
samObj.Text = "FirstText";
req.Add(samObj);

【讨论】:

  • 在类中使用自动属性而不是公共字段
【解决方案2】:

有很多选择,我为你描述其中一些

  1. 使用Dictionary&lt;int, string&gt;
    优点:查找速度非常快
    缺点:你不能有两个相同数字的字符串,你没有List

    var list2d = new Dictionary<int, string>();
    list2d[1] = "hello";
    list2d[2] = "world!";
    foreach (var item in list2d)
    {
        Console.WriteLine(string.Format("{0}: {1}", item.Key, item.Value);
    }
    
  2. 使用Tuple&lt;int, string&gt;
    优点:非常简单方便的工具,你有一个List
    缺点Tuples 是不可变的,一旦创建它们就无法更改它们的值,这会降低代码的可读性(Item1Item2

    var list2d = new List<Tuple<int, string>>();
    list2d.Add(new Tuple(1, "hello"));
    list2d.Add(Tuple.Create(1, "world");
    foreach (var item in list2d)
    {
        Console.WriteLine(string.Format("{0}: {1}", item.Item1, item.Item2);
    }
    
  3. 使用定义的类,
    优点:你有一个List,非常可定制
    缺点:你应该编写更多代码来设置 p>

    public class MyClass
    {
       public int Number { get; set; }
       public string Text { get; set; }
    }
    
    var list2d = new List<MyClass>();
    list2d.Add(new MyClass() { Number = 1, Text = "hello" });
    list2d.Add(new MyClass { Number = 2, Text = "world" });
    foreach (var item in list2d)
    {
        Console.WriteLine(string.Format("{0}: {1}", item.Number, item.Text);
    }
    

【讨论】:

    【解决方案3】:

    自定义类或字典是不错的选择,您也可以使用元组泛型...

    var i = new List<Tuple<int, string>>();
    

    字典要求用作键的任何值都必须是唯一的。所以没有唯一性就不理想。

    如果您不介意添加更多代码,并且如果您决定要在其中添加其他数据,则可以为您提供以后扩展的范围,那么自定义类是更可取的。

    元组既快速又简单,但您会失去可读性并且无法编辑对象。

    【讨论】:

      【解决方案4】:

      用一个字符串和一个 int 属性定义一个类

      public class MyClass 
      { 
        public string MyStr {get;set;} 
        public int MyInt {get;set;} 
      }
      

      然后创建这个类的列表

      List<Myclass> myList = new List<MyClass>();
      
      myList.add(new MyClass{MyStr = "this is a string", MyInt=5});
      

      【讨论】:

      • C# 类考虑 PascalCaseNaming
      【解决方案5】:

      希望对你有帮助

      public class DATA
      {
          public int number;
          public string text;
      }
      
      List<DATA> list = new List<DATA>(); 
      

      【讨论】:

      • DATA 不是类的合适名称,请考虑 PascalCaseNaming
      • @dotctor 编译器不关心你的标识符使用什么大小写。也许您的意思是常见的convention 建议使用驼峰式大小写。
      • 合适!=有效。和不!我建议使用pascal case @PeterM
      • @dotctor suitability 标识符是一种风格和非功能性选择,其中convention 是一个指导方针,而不是绝对的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多