Consider this code:
var x = "tesx".Remove('x');
If I run this code, I get this exception:
startIndex must be less than length of string.
Why can I pass a char instead of an int to this method? Why don't I get a compilation error?
Why does the compiler have this behavior?
you try to remove 'x' which is a declared as char, x is equal to 120
The .Remove
only takes 2 parameters of type int
the start and (optional) count to remove from the string.
If you pass a char, it will be converted to the integer representation. Meaning if you pass 'x' -> 120 is greater than the string's .Length
and that's why it will throw this error!