namespace ConsoleApplication1
{
public static class Foo
{
public static bool StartsWithNum(this string s)
{
for (int i = 0; i < 9; i++)
{
if (s.StartsWith(i.ToString()))
return true;
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
string s = "hello";
Console.WriteLine(s.StartsWithNum());
}
}
}
Scott Gu's blog has a good post explaining extension methods.
In my opinion they are a bad idea because of the following reasons:
- Extension methods encourage that you to patch up your original code from files elsewhere.
- The programmer's "is-a", "has-a" dilemma gets an addition and gives us a trilemma: subclass, wrap or extend?
- Anyone not using Visual Studio 2008 is going to have a tough time navigating through the code as extension methods can be defined in any namespace.
- Programmers may start assuming that the extension method is part of the framework, when it may not be.