最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中循环打印列表变量及当期索引值

变量 crifan 1194浏览 0评论
对于:
            Tuple<double[], double[]> output = crifanDFT.DFT(input);
            double[] outputList1 = output.Item1;
            double[] outputList2 = output.Item2;
对于变量值,其实debug是能看到:
想要去打印出两个浮点数数组的值
用:
            Console.WriteLine(outputList1.ToString());
            Console.WriteLine(outputList2.ToString());
结果是:
System.Double[]
System.Double[]
真垃圾
但是想要打印出来:
然后想要for循环,打印列表1,然后用当期index,去获取到列表2的值
这样成对的值,就可以方便对比查看了
c#  for loop index
c# – How do you get the index of the current iteration of a foreach loop? – Stack Overflow
竟然for循环 获取index 这么麻烦
没有Python的enumerate方便
试了
foreach (var item in Model.Select((value, i) => new { i, value })){
    var value = item.value;
    var index = item.i;}
无法识别Model,放弃。
            foreach (var (item, index) in outputList1.WithIndex())
结果:
CS1061“double[]”未包含“WithIndex”的定义,并且找不到可接受第一个“double[]”类型参数的可访问扩展方法“WithIndex”(是否缺少 using 指令或程序集引用?)
c# – Get current index from foreach loop – Stack Overflow
c# – foreach with index – Stack Overflow
c# – Get current index from foreach loop – Stack Overflow
linq – ForEach with index in C# – Stack Overflow
【总结】
C#中,for循环中,想要获取当前的index索引值:
算了,还是用最原始的index自己去自增吧:
            int curIdx = 0;
            foreach (double curItem1Value in outputList1)
            {
                ...
                curIdx++;
            }

转载请注明:在路上 » 【已解决】C#中循环打印列表变量及当期索引值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.176 seconds, using 22.13MB memory