Equality

Are the variables (or objects) equal.


Operator - ==

Can be used to compare both value and reference data types.
For value types this returns true if the values are equal
For the string reference type this compares the contents.
For all other reference types this compares the identity on the heap.

if (svalue1 == svalue2) 
{
}

'VB.Net Equivalent
If (svalue1 = svalue2) Then
End If

Operators - !=

Are the variables different

if (ovalue1 != null) 
{
}

'VB.Net Equivalent
If ( Not (ovalue Is Nothing) ) Then
End If
If (ovalue IsNot Nothing) Then
End If

Equals() Method

Can be used to compare both value and reference data types.
For all types this compares the contents (not the identity on the heap)
Also known as testing for equality without coalescing.

If (svalue1.Equals(svalue2)) 
{
}

You should always use the Equals() method to check reference types for equality



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext