There is no overload of Remove
that takes a char
, so the character is implicitly converted to an int
and the Remove
method tries to use it as an index into the string, which is way outside the string. That's why you get that runtime error instead of a compile time error saying that the parameter type is wrong.
To use Remove
to remove part of a string, you first need to find where in the string that part is. Example:
var x = "tesx";
var x = x.Remove(x.IndexOf('x'), 1);
This will remove the first occurance of 'x'
in the string. If there could be more than one occurance, and you want to remove all, using Replace
is more efficient:
var x = "tesx".Replace("x", String.Empty);