Alex Rivera | Logout

Result of calling IEquatable<T>.Equals(T obj) when this == null and obj == null?

Asked 2011-11-11T13:54:04.413
8

What should IEquatable<T>.Equals(T obj) do when this == null and obj == null?

1) This code is generated by F# compiler when implementing IEquatable<T>. You can see that it returns true when both objects are null:

    public sealed override bool Equals(T obj)
    {
        if (this == null)
        {
            return obj == null;
        }
        if (obj == null)
        {
            return false;
        }

        // Code when both this and obj are not null.
    }

2) Similar code can be found in the question "in IEquatable implementation is reference check necessary" or in the question "Is there a complete IEquatable implementation reference?". This code returns false when both objects are null.

    public sealed override bool Equals(T obj)
    {
        if (obj == null)
        {
            return false;
        }

        // Code when obj is not null.
    }

3) The last option is to say that the behaviour of the method is not defined when this == null.

Edit
Report

1 Answer

0
answered 2011-11-11T13:57:42.053

Your Answer