If the value of an object is null, does it give an error when calling an extension method?
Person p = null;p. Extendmethod ();
The above code will not be an error, the beginning of this situation is very puzzled, asked Daniel. Daniel explains the following:
The extension function is only intended to make the code more readable, but it will eventually be translated into a standard static function call in the CLR.
Like what:
public static void Extmethod (This string str) { if (!string. IsNullOrEmpty (str)) { //For str processing } }
Call "string". Extmethod () will eventually be translated into Extmethod ("string"); So even if it's null, it won't be an error.
Null why can't I point out the extension function?
Obtain its own method by NULL as follows:
Try this ((string) null). Extmethod () must be able to point it out.
The key is to use the extension function to see the corresponding data type.
C # If the value of an object is null, then why does it not give an error when it calls the extension method