In Java there are “==” and “equals” operator for reference types and “==” for value types. for reference type, “==” means both objects point to the same location and “equals” means their values are the same. does C# has similar operators for value type and reference types?
Share
Well, == can be overloaded for reference types. For example:
Unless it’s overloaded, == means “reference equality” aka “object identity” for reference types. (As Marc says, you may override
Equalswithout overloading ==.)For value types, you have to overload == otherwise the C# compiler won’t let you use it for comparisons. .NET itself will provide an implementation of
Equalswhich usually does the right thing, but sometimes slowly – in most cases, if you write your own custom value type you’ll want to implementIEquatable<T>and overrideEqualsas well – and quite possibly overload various operators.