在实施==
操作员时,我觉得我缺少一些基本要点.
因此,我正在寻找一些最佳实践.
以下是我正在考虑的一些相关问题:
IEquatable<T>
接口实现吗?还是压倒一切 object.Equals
?!=
运营商怎么样?(此列表可能并非详尽无遗).
使用以下代码
public class Task
{
string Name;
public static bool operator ==(Task t1, Task t2)
{ return t1.Name = t2.Name && t1.GetType() == t2.GetType(); }
}
public class TaskA : Task
{
int aThing;
public static bool operator ==(TaskA t1, TaskA t2)
{
return (Task)t1 == (Task)t2 && t1.GetType() == t2.GetType()
&& t1.aThing == t2.aThing; }
}
public class TaskB : Task //more of the same
class Stuffin
{
List<Task> Tasks;
void CheckIt()
{
bool theSame = Tasks[0] == Tasks[1]; …
Run Code Online (Sandbox Code Playgroud) 可能重复:
C#:String.Equals vs. ==
大家好.
有段时间有人告诉我,你永远不应该将字符串与==进行比较,并且你应该使用string.equals(),但它指的是java.
¿.NET c#中的差异beteen ==和string.equals是什么?
在尝试在C#中实现一个简单的单链表时,我注意到==
在比较带有int值的两个对象类型变量但工作时不起作用.Equals
.
想要检查为什么会这样.
以下代码段是通用对象类型Data属性
public class Node {
/// <summary>
/// Data contained in the node
/// </summary>
private object Data { get; set; };
}
Run Code Online (Sandbox Code Playgroud)
下面的代码遍历单链表并搜索对象类型的值 -
/// <summary>
/// <param name="d">Data to be searched in all the nodes of a singly linked list
/// Traverses through each node of a singly linked list and searches for an element
/// <returns>Node if the searched element exists else null </returns>
public Node Search(object d)
{
Node temp …
Run Code Online (Sandbox Code Playgroud) 重载比较运算符,如何比较两个变量是否指向同一个对象(即不是值)
public static bool operator ==(Landscape a, Landscape b)
{
return a.Width == b.Width && a.Height == b.Height;
}
public static bool operator !=(Landscape a, Landscape b)
{
return !(a.Width == b.Width && a.Height == b.Height);
}
Run Code Online (Sandbox Code Playgroud) 错误:
错误C2678:二进制'==':找不到哪个运算符带有'const entry'类型的左操作数(或者没有可接受的转换)
功能:
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T& target) const
{
for (int i = 0; i < maxSize; i++)
if (elements[i] == target) //ERROR???
return i; // target found at position i
// target not found
return -1;
}
Run Code Online (Sandbox Code Playgroud)
这假设是一个重载运算符吗?作为模板类,我不确定我是否理解错误?
解决方案 - 类中的重载函数现在声明为const:
//Operators
bool entry::operator == (const entry& dE) const <--
{
return (name ==dE.name);
}
Run Code Online (Sandbox Code Playgroud) 我被C#中一个奇怪的"不对称"所打击,我真的不明白.请参阅以下代码:
using System;
using System.Diagnostics;
namespace EqualsExperiment
{
class Program
{
static void Main(string[] args)
{
object apple = "apple";
object orange = string.Format("{0}{1}", "ap", "ple");
Console.WriteLine("1");
Debug.Assert(apple.Equals(orange));
Console.WriteLine("2");
Debug.Assert(apple == orange);
Console.WriteLine("3");
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于所有的.NET大师来说,这可能是显而易见的,但第二个断言失败了.
在Java中,我了解到==是这里名为Object.ReferenceEquals的同义词.在C#中,我认为Object.operator ==使用Object.Equals,它是虚拟的,因此它在System.String类中被覆盖.
有人可以解释,为什么第二个断言在C#中失败?我的哪些假设不好?
可能的重复:
Lambda 表达式:== 与 .Equals()
你好,
我经常使用关键字 Equals 来比较变量和其他东西。
但
wines = wines.Where(d => d.Region.Equals(paramRegion)).ToList();
Run Code Online (Sandbox Code Playgroud)
当数据区域为 NULL 时,在运行时返回错误
我不得不使用代码
wines = wines.Where(d => d.Region == paramRegion).ToList();
Run Code Online (Sandbox Code Playgroud)
摆脱错误。
任何想法为什么会引发错误?
谢谢。
我有一个叫做Message
重载这些运算符的类:
public static bool operator ==(Message left, Message right)
public static bool operator !=(Message left, Message right)
public static bool operator ==(Message left, string right)
public static bool operator !=(Message left, string right)
public static bool operator ==(string left, Message right)
public static bool operator !=(string left, Message right)
Run Code Online (Sandbox Code Playgroud)
我要==
和!=
运营商比保持类型的比较参考等String
和Message
,但
var message = new Message();
var isNull = message == null;
Run Code Online (Sandbox Code Playgroud)
给我这个:
以下方法或属性之间的调用不明确:'Message.operator ==(Message,Message)'和'Message.operator ==(Message,string)'
我知道这是因为这两个Message
和String
是引用类型,他们都可以 …
我想将=
等号定义为 Bash 别名,但 Bash 不允许以下任何定义:
alias =='echo foo'
alias \=='echo foo'
alias -- =='echo foo'
alias -- \=='echo foo'
Run Code Online (Sandbox Code Playgroud)
有人知道一种有效的符号吗?