Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# 什么';是什么导致了这个解析错误?ArgumentOutOfRangeException:不能为负。参数名称:长度_C#_Parsing_Unity3d - Fatal编程技术网

C# 什么';是什么导致了这个解析错误?ArgumentOutOfRangeException:不能为负。参数名称:长度

C# 什么';是什么导致了这个解析错误?ArgumentOutOfRangeException:不能为负。参数名称:长度,c#,parsing,unity3d,C#,Parsing,Unity3d,我有一个对象列表和一个delete(),但它正在生成 ArgumentOutOfRangeException: Cannot be negative. Parameter name: length private void delete(int a) { if (currentSelected == -1) return; string str = list.IndexOf(list[currentSelected]).ToString();

我有一个对象列表和一个delete(),但它正在生成

ArgumentOutOfRangeException: Cannot be negative. Parameter name: length 

    private void delete(int a)
    {
        if (currentSelected == -1) return;

        string str = list.IndexOf(list[currentSelected]).ToString();
        Debug.Log("STR: "+str); //returns the correct index
        int id = int.Parse(str.Substring(0, str.IndexOf("\t"))); //Error occurs here
   }

为什么id得到负值?

如果找不到元素,IndexOf将返回-1

显然这里的
str
中没有制表符,因此长度为-1,引发异常

快速检查if有助于:

if (str.Contains('\t'))
{
   ...
}

str.IndexOf(“\t”)返回-1,因为它在找不到值时会这样做:“如果找到该字符,则返回值的从零开始的索引位置;如果找不到,则返回-1。”嗯,我想知道当我从.xml文件加载它时,\t是否被剥离