所以我试图创建的颜色字典的形式创建字典条目包含列表<int>
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]));
非常感谢任何能够帮助我的人! :)
在您的C#代码中不需要'.ToList()'调用。你已经创建了一个新的列表!但是如果你总是要存储三个整数(RGB值),为什么要使用列表的开销?我会考虑使用内置类型,或者创建一个简单的'struct'。 –
我编辑了你的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –
我明白你的观点,我将如何去构造颜色?类似于 colDict.Add(“red”,Color(255,0,0)); ? – mbdavis