I generally work in JavaScript but have been tasked with combing through some C# code that was written several years to replace it.
I came across one example I'm trying to wrap my head around. From my understanding ReferenceEquals()
is used to compare two objects to determine if they are the same instance. But in the code I'm looking at the second parameter passed in is not an object, but null
:
string type = ReferenceEquals(employee.wage_type, null)
? "W2"
: employee.wage_type.Replace(".", string.Empty);
So, my question is, what is this doing here? Is it effectively saying, if employee.wage_type
is equal to null
then assign "W2". If not, take the found value of .
and replace it with an empty string? Does that sound correct?
And if so, wouldn't it have been simpler just to check if the value was null
directly, rather than using ReferenceEquals()
which is meant to compare objects? Or am I misunderstanding what ReferenceEquals()
can/is used for?