Can someone explain to me why in .NET I would write String.IsNullOrEmpty(str) instead of str.IsNullOrEmpty()? There must be a logical reason but I don’t know it.
It sounds like you guys are saying
- You can’t call methods from objects that are null in C#/.NET (I do it in C++, it just doesnt access any member vars)
- Extension methods didn’t exist in .NET 2.0
- Microsoft didn’t bother to update the standards and probably felt it was insignificant
If IsNullOrEmpty were an instance method, calling it on a null instance would throw a NullReferenceException, not return false like you’d want.
It could be an extension method, but then it’d potentially be confusing — it’d look like an instance method, but wouldn’t act like one.