创建数组和访问信息时出错 [英] Errors with creating arrays and accessing information
问题描述
我在这里写了一些代码
I've written some code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tests
{
public class KeyValueStatusGroup<TKey, TValue, TStatus>
{
public TKey Key;
public ValueStatusPair<TValue, TStatus> ValueStatusPair = new ValueStatusPair<TValue, TStatus>();
}
public class ValueStatusPair<TValue, TStatus>
{
public TValue Value;
public TStatus Status;
}
public class TriArray<TKey, TValue, TStatus>
{
private List<KeyValueStatusGroup<TKey, TValue, TStatus>> list = new List<KeyValueStatusGroup<TKey, TValue, TStatus>>();
public ValueStatusPair<TValue, TStatus> this[TKey key]
{
get
{
foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
{
if (keyValueStatusGroup.Key == key)
{
return keyValueStatusGroup.ValueStatusPair;
}
}
}
set
{
foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
{
if (keyValueStatusGroup.Key == key)
{
keyValueStatusGroup.ValueStatusPair = value;
}
}
}
}
}
}
我认为应该有效很好,但我收到的错误是:
运算符'=='不能应用于'TKey'和'TKey'类型的操作数
有什么问题,如何解决?
谢谢杰克
I think it should work fine but I'm getting an error that says:
Operator '==' cannot be applied to operands of type 'TKey' and 'TKey'
What is the problem and how can I fix it???
Thanks Jake
推荐答案
这是因为KeyValuePair< tkey,>没有定义自定义==运算符,并且不包含在可以使用它的预定义值类型列表中。
参考: http://msdn.microsoft.com/en-us/library/53k8ybth%28v=VS.100%29.aspx [ ^ ]
This happens because KeyValuePair<tkey,> does not define a custom == operator and is not included in the predefined list of value types that can use it.
Refer:http://msdn.microsoft.com/en-us/library/53k8ybth%28v=VS.100%29.aspx[^]
对于预定义的值类型,等于运算符(==如果其操作数的值相等则返回true,否则返回false。
For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise.
在这种情况下进行相等检查是最好的选择,因为这不是你可以控制的结构是调用默认值(KeyValuePair< tkey,tvalue>)。等于(对)。
Your best bet for an equality check in this case, because this is not a struct you have control over, is to call default(KeyValuePair<tkey,tvalue>).Equals(pair) instead.
TKey,TValue和TStatus - 当你使用它们时不是真的类型!!!
这是一种声明泛型类型占位符的表示法( http://msdn.microsoft.com/en-us/library/512aeb7t.aspx [ ^ ])。
使用时您的新类型KeyValueStatusGroup(新)您必须声明事实类型而不是TKey,TValue和TStatus ...
TKey, TValue and TStatus - as you use them are NOT real types!!!
This is a kind of notation to declare placeholders for generic types (http://msdn.microsoft.com/en-us/library/512aeb7t.aspx[^]).
When you use your new type KeyValueStatusGroup (new) you have to declare the de-facto types instead of TKey, TValue and TStatus...
如果您将ValueStatusPair属性更改为:
If you change the ValueStatusPair Property to:
public ValueStatusPair<TValue, TStatus> this[TKey key]
{
get
{
foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
{
// use reference equality, not value equality
if (keyValueStatusGroup.Equals(key))
{
return keyValueStatusGroup.ValueStatusPair;
}
}
// all paths must have a return value !
return null;
}
set
{
foreach (KeyValueStatusGroup<TKey, TValue, TStatus> keyValueStatusGroup in list)
{
// use reference equality, not value equality
if (keyValueStatusGroup.Equals(key))
{
keyValueStatusGroup.ValueStatusPair = value;
}
}
}
}
它会编译,但我没有时间测试你的代码,或者弄清楚它的设计。 />
我的印象是你正在实现一个具有某些泛型类型的三个元素的通用元组。
It will compile, but I haven't had time to test your code, or grok its design.
My impression is you are implementing a generic Tuple with three elements of certain generic types.
这篇关于创建数组和访问信息时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!