Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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# 类型为';的未处理异常;系统索引自动失效异常';发生。附加信息:索引超出了数组的边界_C#_.net_Indexoutofboundsexception - Fatal编程技术网

C# 类型为';的未处理异常;系统索引自动失效异常';发生。附加信息:索引超出了数组的边界

C# 类型为';的未处理异常;系统索引自动失效异常';发生。附加信息:索引超出了数组的边界,c#,.net,indexoutofboundsexception,C#,.net,Indexoutofboundsexception,我的代码显示ArrayIndexOutOfRangeException的未处理异常 public static DateTime storeTime(String date) { string[] dateSplit; string[] timeSplit; DateTime returnValue = new DateTime(); if (date == "") return retur

我的代码显示ArrayIndexOutOfRangeException的未处理异常

    public static DateTime storeTime(String date)
    {
        string[] dateSplit;
        string[] timeSplit;
        DateTime returnValue = new DateTime();

        if (date == "")
            return returnValue;

        dateSplit = date.Split(new Char[] { 'T' });
        timeSplit = dateSplit[1].Split(new Char[] { '+' }); // Exception occurs here.
        timeSplit[0] = timeSplit[0].Substring(0, timeSplit[0].Length - 1);
        returnValue = DateTime.ParseExact(dateSplit[0] + " " + timeSplit[0], "yyyy-MM-dd HH:mm:ss",null);

        return returnValue;

    }

您输入的参数
date
不包含任何字符T,因此您的拆分只产生一个元素。因此,尝试访问dateSplit[1](第二个元素)失败。您提供的索引1超出范围,只允许0,因为只有一个元素


请注意,这是一个非常基本的错误,您可能已经被否决,因为您应该能够自己检测到这一点。下次,按F5键调试并检查崩溃时的所有变量,以便您可以自己解决问题。

听起来dateSplit[1]上没有任何元素。。。你在方法中加入了什么参数(日期)。请您调试一下dateSplit中存储的内容,以解决您的问题…您可以粘贴
DateTime
class吗?将Rohit的编辑从[java]更改为[c#]…如果我完成了smth,请更正我。错误的