gpt4 book ai didi

c# - 创建包含 List 的字典条目

转载 作者:太空狗 更新时间:2023-10-30 01:05:46 25 4
gpt4 key购买 nike

所以我正在尝试以以下形式创建颜色字典

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 中的列表放入参数中,例如:

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

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

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

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

最佳答案

我想你需要的是 Dictionary<string, Vector3D> .自己创建一个 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} ;

关于c# - 创建包含 List<int> 的字典条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17437609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com