【问题标题】:create dictionary entry containing List<int>创建包含 List<int> 的字典条目
【发布时间】:2013-07-02 23:50:35
【问题描述】:

所以我正在尝试以表格的形式创建一个颜色字典

Dictionary<string,List<int>> 

像这样:

(colour:colourvals)

例如,如果颜色是红色:

("red":(255,0,0))

我对 c# 很陌生(大约 1 周),但我有相当多的 python 经验。我试图在 python 中实现的效果如下所示:

col_dict = {"red":(255,0,0),
            "blue":(255,0,0),
            "green":(255,0,0)}

回到 c#,经过大量的修补,我终于设法做出了一些可行的东西。这是我当前的代码(看起来很乱):

colDict = new Dictionary<string, List<int>>();
colDict.Add("red", new List<int> { 200, 40, 40 }.ToList());
colDict.Add("green", new List<int> { 40, 200, 40 }.ToList());
colDict.Add("blue", new List<int> { 40, 40, 200 }.ToList());

首先,有没有更好的方法来做到这一点?

其次,然后我想使用列表值作为 Color.FromArgb() 的参数。有什么方法可以将 colDict 中的 List 放入如下参数中:

Color.FromArgb(colDict["green"]);

还是我必须存储颜色选择,然后像这样输入每个值?

this.col = colDict[colour];
Color.FromArgb(this.col[0],this.col[1],this.col[2]));

非常感谢任何可以帮助我的人! :)

【问题讨论】:

  • C# 代码中不需要 .ToList() 调用。您已经在创建new List&lt;int&gt;!但是,如果您要始终存储三个整数(RGB 值),为什么要使用列表的开销呢?我会考虑使用内置类型,或者创建一个简单的struct
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 我明白你的意思,我将如何构建颜色?像 colDict.Add("red", Color (255,0,0)); ?

标签: c# list dictionary ienumerable


【解决方案1】:

我认为您需要的是Dictionary&lt;string, Vector3D&gt;。自己创建一个 Vector3D 类/结构很容易,因为我认为它不随 .NET 框架一起提供:

public class Vector3D
{
  public int R{get;set;}
  public int G{get;set;}
  public int B{get;set;}

}
dict["red"] = new Vector3D{R=255,G=0,B=0} ;

【讨论】:

  • TGH 明白了,非常感谢您的快速回答,我相信您的回答可能也有效,如果我有 15 个代表,我会投票。
【解决方案2】:

根据您的使用情况,您可能会发现将 RGB 颜色存储在单个 int 值中更容易。

public int ColorToInt(byte r, byte g, byte b)
{
   return (b << 16) | (g << 8) | r;
}

您可以使用Color.FromArgb(Int32) 从中获取颜色。因此,您的字典只需存储&lt;Color, int&gt;(或者您可以将Color替换为字符串作为键)

为了节省您每次都调用ColorToInt 方法的时间,您可以创建一个扩展方法。

public static void AddRGB(this Dictionary<Color, int> dict, Color col)
{
   int rgbint = (col.B << 16) | (col.G << 8) | col.R;
   dict.Add(col, rgbint);
}

因此,每当您想向字典中添加项目时,都可以这样做

Dictionary<Color, int> colDict = new Dictionary<Color, int>();
colDict.AddRGB(Color.Green);

它会自动计算Color.Green的int值并将其添加为你的值。

【讨论】:

  • 很棒的答案!非常感谢!
  • 不客气 :) 这里是extension methods 的简要说明,以防您不熟悉它们。
  • 从您的回答中,我实际上想出了一个更简单的方法:制作字典colDict = new Dictionary&lt;string, Color&gt;(); colDict.Add("red", Color.FromArgb(200, 40, 40)); colDict.Add("green", Color.FromArgb(40, 200, 40)); colDict.Add("blue", Color.FromArgb(40, 40, 200)); 现在我可以得到这样的颜色:colDict["red"] 我一定会了解扩展方法。您自动获取颜色 RGB 值的方法是天才,应该提到我希望能够始终指定 RGB 值!谢谢你的帮助!非常感谢。
  • 我按回车键尝试创建一个新行,正如您可能知道的那样,这是我第一次使用 stackoverflow 哈哈哈
  • 没问题,很乐意提供帮助 :) 如果这能解决您的问题,请随时将其作为答案发布并在几天内接受。
【解决方案3】:

您不需要 ToList,因为它已经是一个列表:

            var colDict = new Dictionary<string, List<int>>();
            colDict.Add("red", new List<int> { 200, 40, 40 });
            colDict.Add("green", new List<int> { 40, 200, 40 });
            colDict.Add("blue", new List<int> { 40, 40, 200 });

像这样访问值:

colDict["green"][0]
colDict["green"][1]
colDict["green"][2]


Color.FromArgb(colDict["green"][0],colDict["green"][1],colDict["green"][2]));

创建值的另一种方法是使用元组

var colDict = new Dictionary<string, Tuple<int,int,int>>();
colDict.Add("red", new Tuple<int,int,int>(40,45,49));

 colDict["red"].Item1
 colDict["red"].Item2
 colDict["red"].Item3

【讨论】:

  • 天才,哇,不敢相信我没有尝试过,似乎几乎尝试了其他所有方法!感谢您的快速回复!
【解决方案4】:

最后我想通了,但是没有大家的帮助我是做不到的! 首先使用字符串键和颜色值创建字典:

colDict = new Dictionary<string, Color>(); 

然后可以像这样添加颜色:

colDict.Add("red", Color.FromArgb(200, 40, 40)); 
colDict.Add("green", Color.FromArgb(40, 200, 40)); 
colDict.Add("blue", Color.FromArgb(40, 40, 200)); 

然后可以引用颜色,并且可以像这样作为“颜色”数据类型轻松访问:

colDict["red"]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多