如何在字典中存储一个键和多个值 [英] How to store one key and multiple values in a dictionary
问题描述
嗨朋友们,
我有一个场景,我需要一个带字符串键的字典和相关的两个字符串值,如下所示。
关键是粗体。
我使用Visual Studio 2008,框架3.5。和c#。
字典<字符串,字符串,字符串>
我尝试了什么:
我尝试了以下方法:
1. 字典<字符串,对象>
..........对象封装了我的两个必填字段。
但是在获取值时我将不得不承担额外的投射费用。
2.Someone建议我使用< br $>
字典<字符串,列表><字符串>>
但我猜这个列表本身就是一个对象的集合,也会涉及到投射开销。
所以伙计们请帮帮我....
c#中是否有任何数据结构可以存储多个值作为值类型?
谢谢
Varun
如果你懒得创建字典
string
和Tuple< string,string>
。
IDictionary< string,Tuple< string,string>> myDictionary = new Dictionary< string,Tuple< string,string>>();
如果你'不要懒惰,创建一个包含两个字符串的类(我将称之为 Foo 和 Bar )。
public class MyOwnType {
public string Foo { get ; set ; }
public string 栏{ get 跨度>; set ; }
public MyOwnType( string foo, string bar){
Foo = foo;
Bar = bar;
}
}
IDictionary< string,MyOwnType> myDictionary = new 字典<字符串,MyOwnType>();
希望这有帮助,
Fredrik
根据具体情况,您可以使用至少几种方法,例如:
1.字典< TKey,列表< TValue>>
2.字典< TKey,List< ComplexObject>>
3.字典< TKey,Tuple< TValue1,TValue2>>
等
基于 MSDN文档 [ ^ ],你可以创建一个字典对象来创建文件扩展名和应用程序之间的关系,它可以打开扩展。例如:
Dictionary< string,List< string>> openWith =
new Dictionary< string,List< string>>();
// 在字典中添加一些元素。没有
// 重复键,但有些值是重复的。
openWith.Add( notepad.exe, new 列表< string>(){ .txt,< span class =code-string> 。vbs, CSV跨度>})。
openWith.Add( winword.exe, new 列表< string>(){ .txt, 。vbs, .csv, 。htm, .html, 。doc, 。docx, .dot, .dotx, 。docm, 。dotm});
// 依此类推......
Hi Friends,
I have a scenario where i need to have a dictionary with a string key and associated two string values like the below.
The key is in bold.
I am using Visual studio 2008,framework 3.5. and c#.
dictionary<string,string,string>
What I have tried:
I tried the following approach:
1. dictionary<string,object>
..........where the object encapsulated my two required fields.
But i would have to incur extra casting overhead while fetching the values.
2.Someone suggested me to use
dictionary<string,list><string>>
but i guess the list is itself a collection of objects and will also involve the casting overhead.
So guys please help me out....
is there any datastructure in c# that can store multiple values as valuetypes?
Thanks
Varun
If you're lazy create aDictionary
ofstring
andTuple<string,string>
.
IDictionary<string, Tuple<string, string>> myDictionary = new Dictionary<string, Tuple<string, string>>();
If you're not lazy, create a class that holds your two strings (I'm going to call them Foo and Bar).
public class MyOwnType { public string Foo { get; set; } public string Bar { get; set; } public MyOwnType(string foo, string bar) { Foo = foo; Bar = bar; } }
IDictionary<string, MyOwnType> myDictionary = new Dictionary<string, MyOwnType>();
Hope this helps,
Fredrik
Depending on situation, you can use at least several approaches, such as:
1.Dictionary<TKey, List<TValue>>
2.Dictionary<TKey, List<ComplexObject>>
3.Dictionary<TKey, Tuple<TValue1, TValue2>>
etc.
Based on MSDN documention[^], you can create a dictionary object to create relationship between file extensions and application which is able to open that extensions. For example:
Dictionary<string, List<string>> openWith = new Dictionary<string, List<string>>(); // Add some elements to the dictionary. There are no // duplicate keys, but some of the values are duplicates. openWith.Add("notepad.exe", new List<string>(){".txt", ".vbs", ".csv"}); openWith.Add("winword.exe", new List<string>(){".txt", ".vbs", ".csv", ".htm", ".html", ".doc", ".docx", ".dot", ".dotx", ".docm", ".dotm"}); //and so on...
这篇关于如何在字典中存储一个键和多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!