我有一个字典,其中键是XYZ对象,值是boolean.XYZ类来自Autodesks API,因此它不是我制作的类.我试图检查字典中是否存在密钥.
我的问题:如果字典包含密钥new XYZ(1,1,1),我会检查字典是否包含此密钥,使用myDictionary.ContainsKey(new XYZ(1,1,1)始终返回false.
为什么会发生这种情况?我该如何解决这个问题?我认为该类XYZ需要Equals实现它的方法,但正如我之前提到的,我没有创建这个类,它是Autodesks API的一部分.或者我做错了什么?
Dictionary<XYZ, bool> prevPnts = new Dictionary<XYZ, bool>();
prevPnts[new XYZ(1,1,1)] = true;
// Always says the pnt doesnt exist?
if (prevPnts.ContainsKey(new XYZ(1,1,1)))
TaskDialog.Show("Contains");
else TaskDialog.Show("NOT Contains");
Run Code Online (Sandbox Code Playgroud)
解决方案使用Konrads的答案
class XYZEqualityComparer : IEqualityComparer<XYZ>
{
public bool Equals(XYZ a, XYZ b)
{
if (Math.Abs(a.DistanceTo(b)) <= 0.05)
return true;
return false;
}
public int GetHashCode(XYZ x)
{
int hash = 17;
hash = hash * 23 …Run Code Online (Sandbox Code Playgroud) 对于公共原始变量; 是否有可能实现该set方法和"短实施" get方法?
当我说'Short-Implement'时get我的意思是:
public double width { get; set { width = (isLocked) ? 0:value; } }
Run Code Online (Sandbox Code Playgroud)
而不是'长期实施' get:
public double width { get { return width; } set { width = (isLocked) ? 0:value; } }
Run Code Online (Sandbox Code Playgroud)
当我试图"简短实施" get(这个btw的术语是什么?)和"长期实施" 时,我得到了编译错误set.编译错误是:
`Cube.width.get' must have a body because it is not marked abstract, extern, or partial
Run Code Online (Sandbox Code Playgroud) 有NSIS变量%ALLUSERSPROFILE%吗?
如果不是,您是否知道如何使用NSIS代码获取此环境变量?
注意:如果我使用ReadEnvStr $R7 "ALLUSERSPROFILE",则$R7包含C:/ProgramData因为安装程序已请求提升权限(RequestExecutionLevel admin).这太令人沮丧了!