如何定义运算符= = [英] How to define the operator ==
问题描述
由于类有:
公共类数
{
公众诠释X {得到;组; }
公众诠释Ÿ{搞定;组; }
}
如何定义的重载运算符 ==
,这样我可以使用下面的语句:
数N1 =新的Number {X = 10,Y = 10};
数n2 =新的Number {X = 100,Y = 100};
如果(N1 == N2)
Console.WriteLine(相等);
,否则
Console.WriteLine(不等于);
//更新基于意见如下//
下面是进一步的问题:
在我看来,C#语言做运算符重载区别从C ++的。
在C ++中,此重载运算符是目标类之外定义为一个独立的功能。在这里,在C#中,实际上这个过载功能嵌入到目标类。
有人可以给我一些关于这个论题的意见?
感谢您
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间ConsoleApplication3
{
公共类数
{
公众诠释X {搞定;组; }
公众诠释Ÿ{搞定;组; }
公众号(){}
公众号(INT X,int y)对
{
X = X;
Y = Y;
}
公共静态布尔运算符==(数一,数二)
{
回报率((AX == BX)及和放大器;(AY = =按));
}
公共静态布尔运算符=(数一,数二)
{
回报(A == B);!
}
公共重写字符串的ToString()
{
返回的String.Format(X:{0}; Y:{1},X,Y );
}
公众覆盖布尔等于(obj对象)
{
VAR objectToCompare = OBJ的数量;
如果(objectToCompare == NULL)
返回false;
返回this.ToString()== obj.ToString();
}
公共覆盖INT的GetHashCode()
{
返回this.ToString()GetHashCode()方法。
}
}
类节目
{
静态无效的主要(字符串[] ARG)
{$ B $数量为b N1 =新的Number {X = 10,Y = 10};
数n2 =新的Number {X = 10,Y = 10};
如果(N1 == N2)
Console.WriteLine(相等);
,否则
Console.WriteLine(不等于);
到Console.ReadLine();
}
}
}
公共静态布尔运算符==(YourClassType一,YourClassType b)
{
//做比较
}
更多信息此处。简而言之:
- 是重载的==操作符的任何类型的也应该重载operator = $! b $ b
- 的任何类型的重载操作符==也应该应该覆盖的等于
- 建议重写任何类的equals也覆盖 GetHashCode的
Given the class as follows,
public class Number
{
public int X { get; set; }
public int Y { get; set; }
}
How to define an overload operator ==
so that I can use the following statement:
Number n1 = new Number { X = 10, Y = 10 };
Number n2 = new Number { X = 100, Y = 100 };
if (n1 == n2)
Console.WriteLine("equal");
else
Console.WriteLine("not-equal");
// Updated based on comments as follows //
Here is a further question: It seems to me that C# does operator overload differently from that of C++. In C++, this overloaded operator is defined outside of the targeting class as a standalone function. Here in C#, this overload function in fact is embedded into the targeting class.
Can someone give me some comments on this topic?
Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
public class Number
{
public int X { get; set; }
public int Y { get; set; }
public Number() { }
public Number(int x, int y)
{
X = x;
Y = y;
}
public static bool operator==(Number a, Number b)
{
return ((a.X == b.X) && (a.Y == b.Y));
}
public static bool operator !=(Number a, Number b)
{
return !(a == b);
}
public override string ToString()
{
return string.Format("X: {0}; Y: {1}", X, Y);
}
public override bool Equals(object obj)
{
var objectToCompare = obj as Number;
if ( objectToCompare == null )
return false;
return this.ToString() == obj.ToString();
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
}
class Program
{
static void Main(string[] arg)
{
Number n1 = new Number { X = 10, Y = 10 };
Number n2 = new Number { X = 10, Y = 10 };
if (n1 == n2)
Console.WriteLine("equal");
else
Console.WriteLine("not-equal");
Console.ReadLine();
}
}
}
public static bool operator ==(YourClassType a, YourClassType b)
{
// do the comparison
}
More information here. In short:
- Any type that overloads operator == should also overload operator !=
- Any type that overloads operator == should also should override Equals
- It is recommended that any class that overrides Equals also override GetHashCode
这篇关于如何定义运算符= =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!