Extension method resolution

前端 未结 2 999
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 09:10

I wrote an extension method for String to get a char argument, string.Remove(char). But when I used this, it instead called the default string.Remove(int)

2条回答
  •  盖世英雄少女心
    2021-01-05 09:38

    This behavior is correct. The reason is that introducing an extension method should not change the way existing code executes. Code should behave exactly the same with or without this "superfluous" extension method. It may seem counter-intuitive in certain cases (like yours), but happens for a reason.

提交回复
热议问题