- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有这个简单的例子:
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<MyKey, string> data = new Dictionary<MyKey, string>();
data.Add(new MyKey("1", "A"), "value 1A");
data.Add(new MyKey("2", "A"), "value 2A");
data.Add(new MyKey("1", "Z"), "value 1Z");
data.Add(new MyKey("3", "A"), "value 3A");
string myValue;
if (data.TryGetValue(new MyKey("1", "A"), out myValue))
Console.WriteLine("I have found it: {0}", myValue );
}
}
public struct MyKey
{
private string row;
private string col;
public string Row { get { return row; } set { row = value; } }
public string Column { get { return col; } set { col = value; } }
public MyKey(string r, string c)
{
row = r;
col = c;
}
}
}
这工作正常。但是,如果我以这种方式通过 MyKey 类更改 MyKey 结构:
public class MyKey
然后方法 TryGetValue
没有找到任何 key ,尽管 key 就在那里。
我确信我遗漏了一些明显的东西,但我不知道是什么。
有什么想法吗?
谢谢
(请参阅接受的解决方案以获得更好的 GetHashCode 解析)
我已经像这样重新定义了 MyKey 类,现在一切正常:
public class MyKey
{
private string row;
private string col;
public string Row { get { return row; } set { row = value; } }
public string Column { get { return col; } set { col = value; } }
public MyKey(string r, string c)
{
row = r;
col = c;
}
public override bool Equals(object obj)
{
if (obj == null || !(obj is MyKey)) return false;
return ((MyKey)obj).Row == this.Row && ((MyKey)obj).Column == this.Column;
}
public override int GetHashCode()
{
return (this.Row + this.Column).GetHashCode();
}
}
感谢所有回答此问题的人。
最佳答案
您需要重写 MyKey
类中的 Equals()
和 GetHashCode()
也许是这样的:
GetHashCode()
public override int GetHashCode()
{
return GetHashCodeInternal(Row.GetHashCode(),Column.GetHashCode());
}
//this function should be move so you can reuse it
private static int GetHashCodeInternal(int key1, int key2)
{
unchecked
{
//Seed
var num = 0x7e53a269;
//Key 1
num = (-1521134295 * num) + key1;
num += (num << 10);
num ^= (num >> 6);
//Key 2
num = ((-1521134295 * num) + key2);
num += (num << 10);
num ^= (num >> 6);
return num;
}
}
等于
public override bool Equals(object obj)
{
if (obj == null)
return false;
MyKey p = obj as MyKey;
if (p == null)
return false;
// Return true if the fields match:
return (Row == p.Row) && (Column == p.Column);
}
关于C# 通用字典 TryGetValue 找不到键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976224/
我在 F# 中的字典上使用 TryGetValue,它返回一个对象 bool * Dictionary 我已经用谷歌搜索了几个小时,但是我如何访问这个对象的 bool 组件,以便我可以检查它是否返回了
所以,我有一个 Dictionary Foo 在一个线程中我有: void Thread1() { ... if (!Foo.TryGetValue(key, out v)) { Fo
我正在尝试从字典中获取值(JSON 反序列化)并将其解析为 long。 当我快速查看变量时,我发现没有“M”作为下面给出的输出参数的一部分 但是当我点击这个值时,我发现“M”被添加到给定的值中 这里的
我有一个字典,它将字符串映射到这样的对象:Dictionary myDic; 我事先知道对象的类型是基于字符串的,但我的问题是我应该使用 TryGetValue,还是使用 try、catch 语句直接
将以下代码用于展览 A: string sql; if (!GetQueries.TryGetValue(type.TypeHandle, out sql)) Dictionary 的文档说如果找不到
TryGetValue 是否改变输入参数? 在使用 TryGetValue 时,我倾向于这样做: Dictionary myDic; long lValue = -1; long lTemp1; if
private Dictionary> events = new Dictionary>(); internal Bag GetEventList() where T:class { Ty
我完全是个 C# 菜鸟,无法弄清楚为什么相同的方法以不同的方式工作。我正在制作一个简单的电子表格应用程序,并使用单元格字典,其中键是字符串名称,值是 Cell 对象: public struct Ce
我环顾四周,看到很多关于修改 GetHashCode() 的引用资料和玩ContainsKey()的东西和 TryGetValue() - 但所有这些问题和示例都与一些晦涩的用户特定 key 有关。
我正在编写一个需要优化的计算量大的应用程序(NLP 机器学习任务)。 因为我的代码有很多 for 循环,所以我使用了 Parallel.For(和变体)来并行化最外层的循环。我还使用数组和 Dicti
我有这个简单的例子: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class
private object lockObj = new object(); private Dictionary dict = new Dictionary(); public string Get
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在努力 myDic.TryGetValue("用户名", out user.用户名); 但它不起作用。 这不可能吗? 最佳答案 不,来自文档: “属性不是变量,因此不能作为输出参数传递。” htt
我分析了我的应用程序并运行了一些性能测试,这让我相信以下 if-lock-if 安排: private float GetValue(int id) { float value; if
此代码有效,但效率低下,因为它会重复查找 ignored字典。如何使用字典TryGetValue() LINQ 语句中的方法使其更高效? IDictionary records = ... IDict
所以,给定下面的代码 type MyClass () = let items = Dictionary() do items.Add ("one",1) items.Add (
我有一个类型的字典 Dictionary 尝试从中读取值时,我无法使用这种方式 if (myDict.TryGetValue(1, out (float tupleItem1, float tuple
我有以下ConcurrentDictionary: ConcurrentDictionary sessions; 我知道 sessions.TryGetValue(key, out session)
我不能在匿名类型的 linq 表达式中使用字典中的 TryGetValue()。 Dictionary dict = new Dictionary() { {"1", "one
我是一名优秀的程序员,十分优秀!