Eventuali duplicati:
Why check this != null?Perché String.Equals (Object obj) controlla se questo == null?
// Determines whether two strings match.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public override bool Equals(Object obj)
{
//this is necessary to guard against reverse-pinvokes and
//other callers who do not use the callvirt instruction
if (this == null)
throw new NullReferenceException();
String str = obj as String;
if (str == null)
return false;
if (Object.ReferenceEquals(this, obj))
return true;
return EqualsHelper(this, str);
}
La parte che non capisco è il fatto che si sta controllando per l'istanza corrente, this
, contro il nulla. Il commento è un po 'confuso, quindi mi chiedevo cosa significasse realmente quel commento?
Qualcuno può dare un esempio di come questo potrebbe interrompersi se quell'assegno non fosse presente, e questo significa che dovrei anche inserire tale controllo nelle mie classi?
@FlorianGreinacher: non un possibile duplicato, ma praticamente un duplicato esatto, lol. Mi chiedo perché non è comparso in "Related" quando stavo scrivendo la mia domanda? –