1给定长度 不需要计算长度的 for比foreach循环效率高

2 在不确定长度 或者计算长度有性能损耗的时候 用foreach比较方便

 


        for( int i = arrInt.Count - 1; i >=0; i-- )

        {

            int n = ( int ) arrInt[i];

            if( n == 5 )

                arrInt.RemoveAt( i ); // Remove data here

            Debug.WriteLine( n.ToString() );

        }

 

除了这两个地方外,foreach可以基本适用于任何循环,因此对于循环的编写要尽量使用foreach,因为它会使你的代码清晰简洁,又不失高效。
做什么事贵在坚持, 什么事都怕认真. 只要你认真起来你就能成功!

        for( int i = arrInt.Count - 1; i >=0; i-- )

        {

            int n = ( int ) arrInt[i];

            if( n == 5 )

                arrInt.RemoveAt( i ); // Remove data here

            Debug.WriteLine( n.ToString() );

        }

 

除了这两个地方外,foreach可以基本适用于任何循环,因此对于循环的编写要尽量使用foreach,因为它会使你的代码清晰简洁,又不失高效。

相关文章:

  • 2021-07-11
  • 2022-12-23
  • 2021-08-21
  • 2021-08-21
  • 2022-01-18
  • 2021-08-21
  • 2022-12-23
猜你喜欢
  • 2021-08-25
  • 2022-12-23
  • 2021-08-21
  • 2021-11-02
  • 2021-06-15
  • 2021-09-14
  • 2021-11-02
相关资源
相似解决方案