I just found it in dotPeek, String.cs:
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[__DynamicallyInvokable]
public override bool Equals(object obj)
{
if (this == null)
throw new NullReferenceException();
string strB = obj as string;
if (strB == null)
return false;
if (object.ReferenceEquals((object) this, obj))
return true;
if (this.Length != strB.Length)
return false;
else
return string.EqualsHelper(this, strB);
}
At the second line a NullReferenceException is thrown if this == null. So how does it possible to call method of null object?
MSDN says: Note that applications throw the ArgumentNullException exception rather than the NullReferenceException exception discussed here.
The following Microsoft intermediate language (MSIL) instructions throw NullReferenceException:
callvirt
cpblk
cpobj
initblk
ldelem.<type>
ldelema
ldfld
ldflda
ldind.<type>
ldlen
stelem.<type>
stfld
stind.<type>
throw
unbox
If I get it, exception is thrown before the entry into the method body. Right? So what is need to throw NullReferenceException from method? Does __DynamicallyInvokableAttribute
force method to be called bypassing any checks? Or something else?
Thanks.