这样就可以正常工作(也不例外)。扩展方法不使用虚拟调用(即,它使用“ call” il指令,而不是“
callvirt”),因此除非您在扩展方法中自己编写,否则不存在空检查。实际上,在某些情况下这很有用:
public static bool IsNullOrEmpty(this string value){ return string.IsNullOrEmpty(value);}public static void ThrowIfNull<T>(this T obj, string parameterName) where T : class{ if(obj == null) throw new ArgumentNullException(parameterName);}等等
从根本上讲,对静态调用的调用非常直观-即
string s = ...if(s.IsNullOrEmpty()) {...}变成:
string s = ...if(YourExtensionClass.IsNullOrEmpty(s)) {...}显然没有空检查的地方。



