Monday, February 18, 2008

Untitled

A quick guide to == , Equals(x,y),

 

= =

RefererenceEquals(x,y)

Virtual Equals(x)

Static Equals(x,y)

Value Types

Check for Value Equality

Checks for Reference Equality.

 

Will always return false.

Reason: Boxing is required and hence boxing will result into two different objects.

Overridden for ValueTypes: Hence Checks for Value Equality.

 

But, But, But,,,, using ‘==’ for structs [Again a Value Type] willl result in compliation error as there is no overloaded version is present of ‘==’ for value types.

 

Reference Types

Check for Reference Equality & never checks objects based on their contents.

 

Will return False, if both objects are different.

 

Will return True, if both objects are same, i.e points to same location.

Check for Reference Equality & never checks objects based on their contents.

 

Will return False, if both objects are different.

 

Will return True, if both objects are same, i.e points to same location or both are null.

 

 

Problem: Cannot be overridden to compare objects by value.

 

Check for Reference Equality & never checks objects based on their contents.

 

Will return False, if both objects are different.

 

Will return True, if both objects are same or null, i.e points to same location.

 

 

Problem: Can be overridden to compare objects by value, but will throw an exception if either of them is null.

 

Check for Reference Equality & never checks objects based on their contents.

 

Will return False, if both objects are different. Or either if them is null.

 

Will return True, if both objects are same, i.e points to same location or both are null.

 

 

 

String Types

Same as Value Types

Overridden for String Types:  Hence Checks for Value Equality.

 

Blogged with Flock

No comments: