Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.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# 升序foreach_C# - Fatal编程技术网

C# 升序foreach

C# 升序foreach,c#,C#,我正在尝试检查此数组中的升序 但不知道如何使用foreach foreach (double d in dSize) { if (dSize.ToString() != null) { double dSize1; string str1 = dSize.ToString(); bool success1 = double.TryP

我正在尝试检查此数组中的升序 但不知道如何使用foreach

 foreach (double d in dSize)
        {

            if (dSize.ToString() != null)
            {
                double dSize1;
                string str1 = dSize.ToString();

                bool success1 = double.TryParse(str1, out dSize1);
                if ( dSize < 0.0)
                {
                    errMsg1 = " data grid should contain number >= 0";
                }
                //else
                //{
                //    errMsg1 = " data grid must be entered";
                //}
            }


                *if (inputs.dSize[rowCount] <= inputs.dSize[rowCount - 1])
                {
                    errMsg = "value in row " + (rowCount + 1) + " should be greater than the value in row " + rowCount;
                }
            }*
            swRpt.WriteLine(errMsg);
        }
foreach(dSize中的双d)
{
if(dSize.ToString()!=null)
{
双dSize1;
字符串str1=dSize.ToString();
bool success1=double.TryParse(str1,out dSize1);
如果(dSize<0.0)
{
errMsg1=“数据网格应包含编号>=0”;
}
//否则
//{
//errMsg1=“必须输入数据网格”;
//}
}
*如果(inputs.dSize[rowCount]为什么不

  for(int i = dSize.Length-1; i>=0;i--)
  {
      double d = dSize[i];
      ...
      ...
      ...
  }

您只需要记住从一个迭代到下一个迭代的前一个值。使用foreach进行此操作(与原始迭代器相反)的问题是,第一个值需要一个特例:

Double lastDouble = null;
foreach(double d in dSize) {
    if ((lastDouble != null) && (lastDouble > d)) {
        errMsg = "value out of sequence";
        break;
    }
    lastDouble = d;
}
但是,您已经丢失了错误的行号。您还可以从第一个条目中为最后一个值设置素数,并使用LINQ跳过一个

double lastDouble = dSize.First();
foreach(double d in dSize.Skip(1)) {
    if (lastDouble > d) {
        errMsg = "value out of sequence";
        break;
    }
    lastDouble = d;
}

for
循环在哪里?(你说第二部分使用的是for循环)如果被注释掉了会是什么?星号是什么?使用for循环有什么问题?如果它对你有用,为什么要更改它?那么。假设dSize是某种集合(因为它是你循环的),那么为什么要使用dSize.ToString()?为什么要将其解析为int?另外,请澄清您的问题。有些人不知道您是在尝试从头到尾遍历集合,还是(我认为)只是在尝试检查值是否按升序排列。