Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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#_.net_Dictionary - Fatal编程技术网

C# 确定字典中的最后一个索引引用

C# 确定字典中的最后一个索引引用,c#,.net,dictionary,C#,.net,Dictionary,有没有办法找出字典中引用的最后一个索引?比如说, Dictionary<string,string> exampleDic; ... exampleDic["Temp"] = "ASDF" ... 字典示例; ... 示例[“临时”]=“ASDF” ... 有没有一种方法可以在不将“Temp”存储为变量的情况下以某种方式检索它?没有。没有任何方法可以存储它(这是一个非常不寻常的要求),因此您必须自己执行此操作。实现您自己的字典 public class MyDic : Di

有没有办法找出字典中引用的最后一个索引?比如说,

Dictionary<string,string> exampleDic;

...

exampleDic["Temp"] = "ASDF"

...
字典示例;
...
示例[“临时”]=“ASDF”
...

有没有一种方法可以在不将“Temp”存储为变量的情况下以某种方式检索它?

没有。没有任何方法可以存储它(这是一个非常不寻常的要求),因此您必须自己执行此操作。

实现您自己的字典

public class MyDic : Dictionary<String, String>
{
    public string LastKey { get; set; }

    public String this[String key]
    {
        get
        {
            LastKey = key;
            return this.First(x => x.Key == key).Value;
        }
        set
        {
            LastKey = key;
            base[key] = value; // if you use this[key] = value; it will enter an infinite loop and cause stackoverflow
        }
    }

你为什么不选择通用字典呢:

public class GenericDictionary<K, V> : Dictionary<K, V>
{
    public K Key { get; set; }

    public V this[K key]
    {
        get
        {
            Key = key;
            return this.First(x => x.Key.Equals(key)).Value;
        }
        set
        {
            Key = key;
            base[key] = value;
        }
    }
}
公共类GenericDictionary:字典
{
公钥{get;set;}
公共V本[K键]
{
得到
{
钥匙=钥匙;
返回这个.First(x=>x.Key.Equals(Key)).Value;
}
设置
{
钥匙=钥匙;
基[键]=值;
}
}
}
用法:

Dictionary<string, string> exampleDic;
...
exampleDic["Temp"] = "ASDF"
var key = exampleDic.Key;
字典示例;
...
示例[“临时”]=“ASDF”
变量键=示例键;

Dictionary类没有这样的功能
Dictionary<string, string> exampleDic;
...
exampleDic["Temp"] = "ASDF"
var key = exampleDic.Key;