Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C查询字典的最佳方法#_C#_.net_Asp.net_Dictionary - Fatal编程技术网

C# 用C查询字典的最佳方法#

C# 用C查询字典的最佳方法#,c#,.net,asp.net,dictionary,C#,.net,Asp.net,Dictionary,我有一本字典,例如字典 如果我知道键,那么获取字符串值的最佳方法是什么?您不能执行以下操作: var value=myDictionary[i]?你所说的最佳是什么意思 var stringValue = dictionary[key]; 这是通过键访问字典值的标准方法: var theValue = myDict[key]; 如果密钥不存在,这将引发异常,因此您可能希望在获取密钥之前查看它们是否存在(非线程安全): 或者,您可以使用myDict.TryGetValue,尽管这需要使用out

我有一本字典,例如
字典


如果我知道键,那么获取字符串值的最佳方法是什么?

您不能执行以下操作:


var value=myDictionary[i]

你所说的最佳是什么意思

var stringValue = dictionary[key];
这是通过键访问
字典
值的标准方法:

var theValue = myDict[key];
如果密钥不存在,这将引发异常,因此您可能希望在获取密钥之前查看它们是否存在(非线程安全):


或者,您可以使用myDict.TryGetValue
,尽管这需要使用out参数才能获得值。

字典。TryGetValue
是最安全的方法
string value = dictionary[key];

或者按照其他建议使用Dictionary indexer,但如果您知道密钥在字典中,请记住捕获KeyNotFoundException

value = dictionary[key];
如果您不确定:

dictionary.TryGetValue(key, out value);

嗯,我不太确定你在问什么,但我猜是关于一本字典的

如果知道键,就很容易得到字符串值

string myValue = myDictionary[yourKey];
如果您想像索引器一样使用(如果此词典位于类中),可以使用以下代码

public class MyClass
{
  private Dictionary<string, string> myDictionary;

  public string this[string key]
  {
    get { return myDictionary[key]; }
  }
}
公共类MyClass
{
私人词典;
公共字符串此[字符串键]
{
获取{return myDictionary[key];}
}
}

如果要查询字典集合,可以执行以下操作:

static class TestDictionary 
{
    static void Main() {
        Dictionary<int, string> numbers;
        numbers = new Dictionary<int, string>();
        numbers.Add(0, "zero");
        numbers.Add(1, "one");
        numbers.Add(2, "two");
        numbers.Add(3, "three");
        numbers.Add(4, "four");

        var query =
          from n in numbers
          where (n.Value.StartsWith("t"))
          select n.Value;
    }
}

myDict.TryGetValue效率更高,因为它只需要计算一次键,而不像Contains后跟indexre,indexre会计算两次。@马丁:我打赌键查找的操作是
O(1)
,所以不必担心。反正+1@Martin,但是,它确实需要一个需要事先声明的
out
参数。@zerkms,键查找可能是O(1),但这仍然有效。另外,GetHashCode可能是O(任意),因为它是在key类中实现的。@Oded个人认为,我会在字典上有一个扩展方法,即GetValueOrDefault(key)它在内部使用TryGetValue,如果不存在这样的项,则返回默认值。如果要检查该值,可以在(!dic.TryGetValue(key,out object o)| | o==null)返回时快速说出该

static class TestDictionary 
{
    static void Main() {
        Dictionary<int, string> numbers;
        numbers = new Dictionary<int, string>();
        numbers.Add(0, "zero");
        numbers.Add(1, "one");
        numbers.Add(2, "two");
        numbers.Add(3, "three");
        numbers.Add(4, "four");

        var query =
          from n in numbers
          where (n.Value.StartsWith("t"))
          select n.Value;
    }
}
var evenNumbers =
      from n in numbers
      where (n.Key % 2) == 0
      select n.Value;