Resharper, ICloneable and never null

Resharper complains about the following code, stating that the last zero check is redundant because the expression is always false:

  ICloneable data = item as ICloneable;
  if (data == null)
    throw new InvalidCastException("blah blah, some error message");

  object copy = data.Clone();
  if (copy == null) //  <-- this is where it complains.
    return default(T);

How does he know that he can never be null?

+4
source share
2 answers

ReSharper assumes that your facility adheres to a contract ICloneablethat states, among other things, that

The resulting clone must be of the same type as the original instance, or compatible with it.

, data , , ICloneable.Clone() ReSharper , copy , .

, null Clone. null , .

+3

MSDN:


ICloneable , Clone .

, Clone null.

+2

Source: https://habr.com/ru/post/1544381/


All Articles