Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NET Framework String.Remove(char)方法中存在错误?_C#_.net_String_Overloading - Fatal编程技术网

C# NET Framework String.Remove(char)方法中存在错误?

C# NET Framework String.Remove(char)方法中存在错误?,c#,.net,string,overloading,C#,.net,String,Overloading,因此String.Remove有许多重载,其中之一是:String.Remove(int startIndex) 不知何故,我写的字符'e'被转换成int,调用了错误的重载函数。这完全出乎意料。我是否必须接受这种情况,或者是否有可能提交一个bug,以便在下一版本的(holy).NET framework中得到纠正?没有任何删除方法需要字符 但是,char可以隐式转换为int,因此在您的例子中是这样的。但它实际上不会删除字母e,而是删除索引(int)'e'(在您的情况下,在运行时它将超出范围)处

因此
String.Remove
有许多重载,其中之一是:
String.Remove(int startIndex)


不知何故,我写的字符
'e'
被转换成
int
,调用了错误的重载函数。这完全出乎意料。我是否必须接受这种情况,或者是否有可能提交一个bug,以便在下一版本的(holy).NET framework中得到纠正?

没有任何
删除
方法需要
字符

但是,
char
可以隐式转换为
int
,因此在您的例子中是这样的。但它实际上不会删除字母
e
,而是删除索引
(int)'e'
(在您的情况下,在运行时它将超出范围)处的字符

如果要“删除”字母
e
,则:

("hello").Remove('e');
我预测将来可能会出现字符串不变性的情况。祝你好运;-)

具有精确的2重载,这两个重载都将
int
作为其第一个参数

我相信你正在寻找,就像在


Remove将整数作为其参数,而不是字符e'变成101作为一个整数。

请查看intellisense的方法:它是:

string newString = "hello".Replace("e", string.Empty);
它言行一致;这不是你想要的方法。你想要的是:

    //
    // Summary:
    //     Returns a new string in which all the characters in the current instance,
    //     beginning at a specified position and continuing through the last position,
    //     have been deleted.
    //
    // Parameters:
    //   startIndex:
    //     The zero-based position to begin deleting characters.
    //
    // Returns:
    //     A new string that is equivalent to this string except for the removed characters.
    //
    // Exceptions:
    //   System.ArgumentOutOfRangeException:
    //     startIndex is less than zero.-or- startIndex specifies a position that is
    //     not within this string.
    public string Remove(int startIndex);
你的问题是什么

由于没有将
char
作为参数的重载,因此不能期望以这种方式删除
'e'

只需使用
string.Replace(string,string)
string.Remove()只有两个重载,其中一个重载接受int参数(没有一个重载接受char参数)

字符可以很容易地转换为整数

因此,调用string.Remove(int)


不是虫子。:)

这是因为隐式类型将char转换为int的设计决策错误。@TimSchmelter-没有接受char的Remove方法。@ChrisF:no,但是接受
int
char
的方法可以隐式转换为
int
。这就是为什么上面的代码会编译,但会抛出一个runtimew异常,因为它超出了范围。.网友们,如果我错了,你们可以杀了我,然后否决并关闭这个问题和所有内容[…]呵呵,很好的介绍。别误会,我也是一个粉丝,但介绍是独一无二的…:这里有一些推理和逻辑:
    //
    // Summary:
    //     Returns a new string in which all the characters in the current instance,
    //     beginning at a specified position and continuing through the last position,
    //     have been deleted.
    //
    // Parameters:
    //   startIndex:
    //     The zero-based position to begin deleting characters.
    //
    // Returns:
    //     A new string that is equivalent to this string except for the removed characters.
    //
    // Exceptions:
    //   System.ArgumentOutOfRangeException:
    //     startIndex is less than zero.-or- startIndex specifies a position that is
    //     not within this string.
    public string Remove(int startIndex);
string s = "hello".Replace("e","");