[英]Operator Overloading in C# for a VB.NET person
Consider This Class which overloads the = Operator for the Person Class. 考虑这个类重载了Person类的=运算符。 This works in VB.NET:
这适用于VB.NET:
Public Class Person
Public Sub New(ByVal FirstName As String, ByVal LastName As String)
_FirstName = FirstName
_LastName = LastName
End Sub
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
Public Shared Operator <>(ByVal Person1 As Person, ByVal Person2 As Person) As Boolean
Return Not (Person1 = Person2)
End Operator
Public Shared Operator =(ByVal Person1 As Person, ByVal Person2 As Person) As Boolean
Return Person1.FirstName = Person2.FirstName AndAlso Person1.LastName = Person2.LastName
End Operator
Public Class Test
Public Sub TestIt()
Dim P1 As New Person("f1", "l1")
Dim P2 As Person = Nothing
Dim IsEqual As Boolean = False
If P1 IsNot Nothing AndAlso P2 IsNot Nothing Then
If P1 = P2 Then
IsEqual = True
End If
End If
End Sub
End Class
End Class
And the same class in C#: 和C#中的同一个类:
public Person(string FirstName, string LastName)
{
_FirstName = FirstName;
_LastName = LastName;
}
private string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public static bool operator !=(Person Person1, Person Person2)
{
return !(Person1 == Person2);
}
public static bool operator ==(Person Person1, Person Person2)
{
return Person1.FirstName == Person2.FirstName && Person1.LastName == Person2.LastName;
}
public class Test
{
public void TestIt()
{
Person P1 = new Person("f1", "l1");
Person P2 = null;
bool IsEqual = false;
if (P1 != null && P2 != null)
{
if (P1 == P2)
{
IsEqual = true;
}
}
}
}
Using the C# class, how can I test to see if a Person
Object is null
before calling the overloaded ==
operator function if the function is used in testing to see if it is null
. 使用C#类,如何测试,看是否有
Person
对象是null
调用重载之前==
操作符的功能,如果功能是用于测试,看它是否为null
。 That is, is there an equivalent counterpart function to VB's Is Nothing
that is separate from the ==
test? 也就是说,是否存在与VB的
Is Nothing
相当的对应函数,它与==
测试分开? It seems like C# uses ==
for both. 似乎C#使用
==
两者。
I tried object.IsEqual
; 我试过
object.IsEqual
; that also looked wrong. 那看起来也错了。
BTW, since you're implementing ==
you should also override Equals (and GetHashCode). 顺便说一句,既然你正在实现
==
你也应该重写Equals(和GetHashCode)。
A simple way to do this is to implement IEquatable<Person>
: 一个简单的方法是实现
IEquatable<Person>
:
public bool Equals(Person other)
{
return other != null &&
FirstName == other.FirstName &&
LastName == other.LastName;
}
Then override Equals: 然后覆盖等于:
public override bool Equals(object o)
{
var other = o as Person;
return other != null && this.Equals(other);
}
Finally ==
is just: 最后
==
只是:
public static bool operator ==(Person left, Person right)
{
return object.Equals(left, right);
}
public static bool operator !=(Person left, Person right)
{
return !(left == right);
}
( object.Equals hands null
cases correctly.) ( object.Equals正确地处理
null
案例。)
Check for null
in your overloaded operator too using object.ReferenceEquals
: 使用
object.ReferenceEquals
检查重载运算符中的null
:
public static bool operator ==(Person Person1, Person Person2)
{
if(object.ReferenceEquals(Person1, null)) {
return object.ReferenceEquals(Person2, null);
} else if(object.ReferenceEquals(Person2, null)) {
return false;
} else {
return Person1.FirstName == Person2.FirstName && Person1.LastName == Person2.LastName;
}
}
Or, more concisely: 或者,更简洁:
return object.ReferenceEquals(person1, person2) || (
!object.ReferenceEquals(person1, null)
&& !object.ReferenceEquals(person2, null)
&& person1.FirstName == person2.FirstName
&& person1.LastName == person2.LastName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.