【C#】干掉for循环
2010-09-30 22:45:20 来源:WEB开发网注:对于嵌套的for循环,就用SelectMany!
声明:for循环很好,你可以继续用,如果你想用的话。如果你喜欢尝试新东西,我想告诉你:"这也许是应该的!"
附录1:乘法口诀
1 x 1= 1
1 x 2= 2 2 x 2= 4
1 x 3= 3 2 x 3= 6 3 x 3= 9
1 x 4= 4 2 x 4= 8 3 x 4=12 4 x 4=16
1 x 5= 5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=25
1 x 6= 6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36
1 x 7= 7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7=49
1 x 8= 8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x 8=48 7 x 8=56 8 x 8=64
1 x 9= 9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81
附录2:完整代码
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace KSharp
{
[TestFixture]
public class TestForLoop
{
[Test]
public void OldSum()
{
int sum0 = 0;
for (int i = 0; i < 10; i++)
{
sum0 += i;
}
Assert.AreEqual(45, sum0);
}
[Test]
public void NewSum()
{
int sum1 = Enumerable.Range(0, 10).Sum();
int sum2 = Enumerable.Range(0, 10).Aggregate((x, y) => x + y);
int sum3 = Enumerable.Range(0, 10).Aggregate(0, (x, y) => x + y);
Assert.AreEqual(45, sum1);
Assert.AreEqual(45, sum2);
Assert.AreEqual(45, sum3);
}
[Test]
public void OldFilter()
{
int[] arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> odd_list = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % 2 == 1)
{
odd_list.Add(arr[i]);
}
}
int[] odd_arr = odd_list.ToArray();
Assert.That(odd_arr, Is.EquivalentTo(new int[] { 1, 3, 5, 7, 9 }));
}
[Test]
public void NewFilter()
{
int[] arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] odd_arr = arr.Where(x => x % 2 == 1).ToArray();
Assert.That(odd_arr, Is.EquivalentTo(new int[] { 1, 3, 5, 7, 9 }));
}
[Test]
public void OldMap()
{
int[] arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> new_list = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
new_list.Add(arr[i] * 10);
}
int[] new_arr = new_list.ToArray();
Assert.That(new_arr, Is.EquivalentTo(new int[] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 }));
}
[Test]
public void NewMap()
{
int[] arr = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] new_arr = arr.Select(x => x * 10).ToArray();
Assert.That(new_arr, Is.EquivalentTo(new int[] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 }));
}
[Test]
public void PrintMultiplicationFact()
{
Console.Write(
" 1 x 1= 1 \n"
+ " 1 x 2= 2 2 x 2= 4 \n"
+ " 1 x 3= 3 2 x 3= 6 3 x 3= 9 \n"
+ " 1 x 4= 4 2 x 4= 8 3 x 4=12 4 x 4=16 \n"
+ " 1 x 5= 5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=25 \n"
+ " 1 x 6= 6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36 \n"
+ " 1 x 7= 7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7=49 \n"
+ " 1 x 8= 8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x 8=48 7 x 8=56 8 x 8=64 \n"
+ " 1 x 9= 9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81 \n"
);
/*********************方法一: 嵌套循环*************************/
for (int j = 1; j < 10; j++)
{
for (int i = 1; i < 10; i++)
{
if (i <= j)
{
Console.Write("{0, 2} x{1, 2}={2, 2}\t", i, j, i * j);
}
}
Console.Write("\n");
}
/*********************方法二: 扩展方法*************************/
Enumerable.Range(1, 9)
.SelectMany(j => Enumerable.Range(1, 9), (j, i) => new { i, j })
.Where(x => x.i <= x.j)
.GroupBy(x => x.j)
.Select(g => g.Aggregate("", (a, x) => a + string.Format("{0, 2} x{1, 2}={2, 2}\t", x.i, x.j, x.i * x.j)))
.ToList().ForEach(x => Console.WriteLine(x));
/*********************方法三: Linq表达式************************/
(
from j in Enumerable.Range(1, 9)
from i in Enumerable.Range(1, 9)
where i <= j
group new { i, j } by j into g
select new
{
LineNo = g.Key,
Line = g.Aggregate("", (a, x) => a + string.Format("{0, 2} x{1, 2}={2, 2}\t", x.i, x.j, x.i * x.j))
}
).ToList().ForEach(g => Console.WriteLine(g.Line));
}
}
}
- ››Form Reset Function
- ››FOREACH 宏之GCC实现
- ››循环不间断向上滚动的文本特效代码
- ››ForeFront让企业VPN连接高枕无忧
- ››Forefront TMG日志记录功能
- ››Forefront Security For Exchange文件传送限制测试...
- ››Forefront Security For Exchange关键字测试
- ››Forefront Security For Exchange的反病毒测试
- ››Forefront Security For Exchange部署指南
- ››Forrester:惠普数据中心自动化解决方案最佳
- ››循环链表实验
- ››Forte for Java简介与基础安装
更多精彩
赞助商链接