I was going through the OperatingSystem.cs file in the .NET reference source and noted this code in line 50:
if ((Object) version == null)   version is an object of class Version, which means version derives from Object. If that is so, isn't it redundant casting to Object? Wouldn't it be the same as this?
if (version == null) 
                No, it's not equivalent - because Version overloads the == operator.
The snippet which casts the left operand to Object is equivalent to:
if (Object.ReferenceEquals(version, null))   ... rather than calling the operator== implementation in Version. That's likely to make a nullity check as its first action anyway, but this just bypasses the extra level.
In other cases, this can make a very significant difference. For example:
string original = "foo"; string other = new string(original.ToCharArray()); Console.WriteLine(original == other); // True Console.WriteLine((object) original == other); // False 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With