Consider This Class which overloads the = Operator for the Person Class. This works in 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#:
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
. That is, is there an equivalent counterpart function to VB's Is Nothing
that is separate from the ==
test? It seems like C# uses ==
for both.
I tried object.IsEqual
; that also looked wrong.
BTW, since you're implementing ==
you should also override Equals (and GetHashCode).
A simple way to do this is to implement 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.)
Check for null
in your overloaded operator too using object.ReferenceEquals
:
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.