Monday, March 17, 2014

Equals and "==" operator in C#

Came across an interesting blog here:

http://blogs.msdn.com/csharpfaq/archive/2004/03/29/102224.aspx

In summary:

Equals and '==' operator in C# is an interesting topic. It is Polymorphism behind this.

Reference type: by default (which means no Equals overriding or '==' operator overloading) both of them do reference comparison.

Value type: by default it doesn't support '==' operator. If you define a struct and use '==' operator to compare its two instances, it can't pass the compilation. You need to overload '==' operator in the struct. Equals checks the values equality.

Equals is overriding and '==' operator is overloading. Remember this will be helpful for you to understand it.

No comments:

Post a Comment