在C系列語言中
在裡面
編程語言一直在進化
在C#
對於用慣了for循環的朋友
下面我用代碼來演示
注
聲明
附錄
附錄
代碼
using System;
using System
using System
using System
using NUnit
namespace KSharp
{
[TestFixture]
public class TestForLoop
{
[Test]
public void OldSum()
{
int sum
for (int i =
{
sum
}
Assert
}
[Test]
public void NewSum()
{
int sum
int sum
int sum
Assert
Assert
Assert
}
[Test]
public void OldFilter()
{
int[] arr = new[] {
List<int> odd_list = new List<int>();
for (int i =
{
if (arr[i] %
{
odd_list
}
}
int[] odd_arr = odd_list
Assert
}
[Test]
public void NewFilter()
{
int[] arr = new[] {
int[] odd_arr = arr
Assert
}
[Test]
public void OldMap()
{
int[] arr = new[] {
List<int> new_list = new List<int>();
for (int i =
{
new_list
}
int[] new_arr = new_list
Assert
}
[Test]
public void NewMap()
{
int[] arr = new[] {
int[] new_arr = arr
Assert
}
[Test]
public void PrintMultiplicationFact()
{
Console
+
+
+
+
+
+
+
+
);
/*********************方法一: 嵌套循環*************************/
for (int j =
{
for (int i =
{
if (i <= j)
{
Console
}
}
Console
}
/*********************方法二: 擴展方法*************************/
Enumerable
/*********************方法三: Linq表達式************************/
(
from j in Enumerable
from i in Enumerable
where i <= j
group new { i
select new
{
LineNo = g
Line = g
}
)
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/12625.html