Why does this happen? Please observe the following code:
static class StringExtension
{
public static string Remove(this string s, char c)
{
return s.Replace(c.ToString(), "");
}
public static string Remove(this string s, char[] a)
{
foreach (char c in a)
{
s = s.Remove((char)c); // <---- ArgumentOutOfRange Exception here
}
return s;
}
}
class Program
{
static void Main(string[] args)
{
char[] a = new char[] { '.', ',' };
string testString = "Clean.this,string.from,periods.and,commas.";
Console.WriteLine(testString.Remove(a));
}
}
When I run this code, I get an ArgumentOutOfRange exception at the indicate line. Turns out that even if I have a specific code for an extension Remove (this, char) and I explicitly (although, there should be no reason for this) specify the parameter’s type, it ignores my extension and tries to call the original Remove(int) method.
Am I doing something wrong or this is a bug in C#?
P.S. I use VS2010.
This line:
is calling
string.Remove(int)– the compiler will always use an applicable instance method instead of an extension method if it can. It’s applicable due to the implicit conversion fromchartoint. That’s the method that’s throwing the exception, because the argument you’re passing it is out of range. (In fact, you’re lucky – in a worse situation it would be in range, and returning entirely unexpected results.)In general I would strongly advise you not to create extension methods which have the same names as instance methods on the extended type, particularly if they’ve got the same number of parameters. Working out overloading is hard enough in general without adding extension methods to the mix. Don’t forget that whenever you can’t easily work out what your code is doing, someone reading the code in a year’s time is going to have a ten times harder job.