一键多个值在C#字典? [英] One Key to multiple values dictionary in C#?
问题描述
我经常需要一键多vaules字典,但在C#中大部分都是两个维度像字典和Hashtable
I often need one key to multiple vaules dictionary, but in C# most of them are two dimensions like Dictionary and Hashtable.
我想是这样的:
var d = new Dictionary<key-dt,value1-dt,value2-dt,value3-dt,value4-dt>();
DT内<>表示数据类型。任何人有这个想法?
dt inside<> means data type. Anybody has ideas about this?
推荐答案
一个字典是一个键值对,其中的值被取取决于关键。钥匙都是独一无二的。
A dictionary is a key-value pair, where the value is fetched depending on the key. The keys are all unique.
现在,如果你想有一个词典
1公钥类型和多值类型,你有几个选项:
Now if you want a Dictionary
with 1 keytype and multiple value types, you have a few options:
首先是使用元组
VAR字典=新词典<关键字类型,元组LT;字符串,字符串,BOOL,INT>>()
另一种是使用(用C#4.0及以上):
The other is to use (with C# 4.0 and above):
VAR字典=新词典<关键字类型,动态>()
System.Dynamic.ExpandoObject
可以拥有任何类型的值。
the System.Dynamic.ExpandoObject
can have value of any type.
using System;
using System.Linq;
using System.Collections.Generic;
public class Test {
public static void Main(string[] args) {
dynamic d1 = new System.Dynamic.ExpandoObject();
var dict = new Dictionary<int, dynamic>();
dict[1] = d1;
dict[1].FooBar = "Aniket";
Console.WriteLine(dict[1].FooBar);
dict[1].FooBar = new {s1="Hello", s2="World", s3=10};
Console.WriteLine(dict[1].FooBar.s1);
Console.WriteLine(dict[1].FooBar.s3);
}
}
这篇关于一键多个值在C#字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!