C# has extension methods .
Some static methods of the string
class can be rewritten as extension methods eg String.IsNullOrEmpty .
Is there a reason why there are no default extension method for the string
class that provide the same functionality as the static class?
Yes. A really good one.
It PREDATES the existence of extension methods. It simply was there first.
There are quite some similar scenarios around. Lots of API taking a typeof but not having a generic version, ie - all predating generics.
Look at the string.IsNullOrEmpty documentation (specifically, the "Applies to" section).
This method existed since .NET Framework 2.0.
If you look at the history of C# from the official docs , you can simply find that extension methods came with C# 3.0, which was around .NET Framework 3.5.
Note: Don't mix between C# versions and the .NET versions.
Another reason is the method would fail if the string reference was null. Consider something like this:
string x = null;
if (x.isNullOrEmpty()) ...
This call would obviously cause an error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.