Person クラスの = 演算子をオーバーロードするこのクラスを検討してください。これは 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
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;
}
}
}
}
C# クラスを使用して、オーバーロードされた演算子関数を呼び出す前に、Person
関数null
が. つまり、テストとは別の VB に対応する同等の関数はありますか? C#は両方に使用しているようです。==
null
Is Nothing
==
==
やってみobject.IsEqual
ました; それも間違って見えました。