C# 我试图在Dictionary<;上定义/运算符有什么错误;字符串,int>;?
我正在尝试做一个函数,它取两个C# 我试图在Dictionary<;上定义/运算符有什么错误;字符串,int>;?,c#,.net,algorithm,data-structures,C#,.net,Algorithm,Data Structures,我正在尝试做一个函数,它取两个字典的差,例如 { "SomeKey" -> 4 , "SomeOtherKey" -> 2 } / { "SomeKey" -> 1, "Keykeykey" -> 69 } = { "SomeKey -> 3" , "SomeOtherKey" -> 2 } 我的程序是 public static Dictionary<string, int> operator / ( Dictionary<str
字典的差,例如
{ "SomeKey" -> 4 , "SomeOtherKey" -> 2 } / { "SomeKey" -> 1, "Keykeykey" -> 69 }
= { "SomeKey -> 3" , "SomeOtherKey" -> 2 }
我的程序是
public static Dictionary<string, int> operator / ( Dictionary<string, int> first, Dictionary<string, int> second )
{
// Returns all elements in first but not second, where the number in the first but not second
// for a given key is the first's value minus the second's value
Dictionary<string, int> firstNotSecond = new Dictionary<string, int>();
foreach ( KeyValuePair<string, int> pair in first )
{
int secondNum = second.ContainsKey(pair.Key) ? second[pair.Key] : 0;
if ( pair.Value > secondNum )
{
firstNotSecond[pair.Key] = pair.Value - secondNum;
}
}
return firstNotSecond;
}
公共静态字典操作符/(字典第一,字典第二)
{
//返回第一个而非第二个元素中的所有元素,其中数字位于第一个而非第二个元素中
//对于给定的键,第一个键的值减去第二个键的值
Dictionary firstNotSecond=新字典();
foreach(第一个为KeyValuePair对)
{
int secondNum=second.ContainsKey(pair.Key)→second[pair.Key]:0;
if(pair.Value>secondNum)
{
firstNotSecond[pair.Key]=pair.Value-secondNum;
}
}
返回第一而不是第二;
}
当我试着把它用在箱子里的时候
Dictionary<string, int> friendsGained = latestTwo[0].names / latestTwo[1].names,
friendsLost = latestTwo[1].names / latestTwo[0].names;
Dictionary-friendsgeed=latestTwo[0]。name/latestTwo[1]。name,
friendsLost=latestTwo[1]。名称/latestTwo[0]。名称;
我得到了编译时错误
运算符“/”不能应用于类型为的操作数
“System.Collections.Generic.Dictionary
”和
“System.Collections.Generic.Dictionary
”
给出了什么?正如其他人建议的那样,您需要将您的类作为操作符的一部分
public class YourClass
{
public Dictionary<string, int> YourProperty...
public static Dictionary<string, int> operator / ( YourClass first, Dictionary<string, int> second )
{
// Returns all elements in first but not second, where the number in the first but not second
// for a given key is the first's value minus the second's value
Dictionary<string, int> firstNotSecond = new Dictionary<string, int>();
foreach ( KeyValuePair<string, int> pair in first.YourProperty )
{
int secondNum = second.ContainsKey(pair.Key) ? second[pair.Key] : 0;
if ( pair.Value > secondNum )
{
firstNotSecond[pair.Key] = pair.Value - secondNum;
}
}
return firstNotSecond;
}
}
公共类您的类
{
公共字典属性。。。
公共静态字典运算符/(YourClass第一,字典第二)
{
//返回第一个而非第二个元素中的所有元素,其中数字位于第一个而非第二个元素中
//对于给定的键,第一个键的值减去第二个键的值
Dictionary firstNotSecond=新字典();
foreach(first.YourProperty中的KeyValuePair对)
{
int secondNum=second.ContainsKey(pair.Key)→second[pair.Key]:0;
if(pair.Value>secondNum)
{
firstNotSecond[pair.Key]=pair.Value-secondNum;
}
}
返回第一而不是第二;
}
}
当实现像/
这样的二进制运算符时,至少应该有一个参数属于您要实现的类
公共类MyClass{
...
//不编译:
//“first”或“second”应为MyClass类型
公共静态字典运算符/
(字典第一,字典第二){…}
...
}
我建议使用扩展方法,例如
公共静态字典扩展{
公共静态字典减法(此IDictionary优先,
词典第二版){
if(null==第一个)
返回null;//或抛出新的ArgumentNullException(“first”);
else if(null==秒)
首先返回.ToDictionary(pair=>pair.Key,pair=>pair.Value);//将其复制
//或者抛出新的异常(“第二个”);
字典结果=新字典();
foreach(第一个变量对){
int secondNum;
if(second.TryGetValue(pair.Key,out secondNum))
如果(pair.Value>secondName)
添加(pair.Key,pair.Value-secondNum);
}
返回结果;
}
}
所以你可以把它
Dictionary<string, int> friendsGained = latestTwo[0].names.Subtract(latestTwo[1].names);
Dictionary<string, int> friendsLost = latestTwo[1].names.Subtract(latestTwo[0].names);
Dictionary-friendsgeed=latestTwo[0]。名称。减法(latestTwo[1]。名称);
Dictionary friendsLost=latestTwo[1]。名称。减法(latestTwo[0]。名称);
对于这种复杂且更重要且相当混乱的机制,不应使用运算符重载。创建一个方法来描述您希望通过此操作实现的目标。此外,静态定义的运算符意味着您可以将每一个类型为string,int
的字典与另一个类型为whrong的字典进行区分。运算符定义上没有编译器错误吗?你在哪门课上写?您应该会收到错误,因为没有一个操作数是您的类的类型。您只能在类C中重载类C的运算符。因此您必须编辑字典的源代码。写一个(n)(扩展)方法。我认为这比写一个像dict(字典第一,字典第二)这样的函数更优雅。
是我试图让它更“优雅”的唯一原因编写一个方法DicDifference
或SubtractValuesByKey
,而不是提供一个没有人会理解甚至注意到的运算符。
public static DictionaryExtensions {
public static Dictionary<K, int> Subtract(this IDictionary<K, int> first,
IDictionary<K, int> second) {
if (null == first)
return null; // or throw new ArgumentNullException("first");
else if (null == second)
return first.ToDictionary(pair => pair.Key, pair => pair.Value); // let it be copy
// or throw new ArgumentNullException("second");
Dictionary<K, int> result = new Dictionary<K, int>();
foreach (var pair in first) {
int secondNum;
if (second.TryGetValue(pair.Key, out secondNum))
if (pair.Value > secondName)
result.Add(pair.Key, pair.Value - secondNum);
}
return result;
}
}
Dictionary<string, int> friendsGained = latestTwo[0].names.Subtract(latestTwo[1].names);
Dictionary<string, int> friendsLost = latestTwo[1].names.Subtract(latestTwo[0].names);