c# - 无法访问的代码 C#

标签 c# winforms for-loop unreachable-code

做一些 C# 循环和 if 语句。突出显示 for 循环中无法访问的代码。我不太明白这个 1。

        public bool checkTime()
    {

        // Read values back from Json file
        var serializedList = File.ReadAllText(@filePathTimes);

        // getting a list of LockTime objects
        List<LockTime> deserializedList = (List<LockTime>)JsonConvert.DeserializeObject(serializedList, typeof(List<LockTime>));

        if (deserializedList.Count != 0)
        {

                // Grab whatever data you want from this list to store somewhere, such as a list of all Start and End integers.
                List<DateTime> intStartList = deserializedList.Select(entry => entry.Start).ToList();
                List<DateTime> intEndList = deserializedList.Select(entry => entry.End).ToList();

                //Then I do a foreach loop to go through every value in the start list and add the same located value to my listOfTimes (the list of LockTime objects with start and end)
                for (int x = 0; x < intStartList.Count; x++)
                {
                    TimeSpan start = new TimeSpan(intStartList[x].Hour, intStartList[x].Minute, intStartList[x].Second);
                    TimeSpan end = new TimeSpan(intEndList[x].Hour, intEndList[x].Minute, intEndList[x].Second);
                    TimeSpan now = DateTime.Now.TimeOfDay;
                    LockTime theTime = new LockTime(intStartList[x], intEndList[x]);
                    _lockTimes.Add(theTime);
                    if((now > start) && (now < end))
                    {
                        return true;
                    }

                        return false;

                }
        }
        return false;
    }

不可访问代码的亮点出现在 for 循环的 x++ 上。知道这是为什么吗?

最佳答案

那是因为无论发生什么,循环内的代码都会执行 return true;return false;

循环不会循环

关于c# - 无法访问的代码 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23358869/

相关文章:

java - for 循环中的循环问题,同时避免重复条目

c# - 如何等到调用 Web 服务完成?

c# - 非网络环境中的第一个 "programs"

java - 代码似乎读取正确,但无法运行

php 简单函数中的未定义偏移量()

c# - silverlight 3.0与winforms的通信

c# - 如何使用 Unity 将基于 2D 数组的图 block 实例化到平台游戏中?

c# - 解析器如何处理泛型类型参数?

c# - 为什么 asp.net 不会删除我的行?

C#:DataGridView 数据源更新 - DataTable、List、BindingList 和 BindingSource?