| 日历 |
|
|
| 网志分类 |
|
|
|
| 站内搜索 |
|
| 友情链接 |
|
0004780
|
|
 |
MeteorX的白板
胡言乱语中...
|
|
|
以前一直都没怎么去研究那些东西,前几天看了些Python,对例如"[x * x for x in a]"之类的语句很有感触
今天在vs05起始页里面才看到c#里面System也已经内建了类似的功能
现在有些理解delegate的必要性了,以前一直认为delegate所实现的功能完全是interface的一个子集
现在想想,有时候delegate的确能大大提高code的简洁性,一味oop是不对的
贴代码~~~~:
class Program
{
static void Main(string[] args)
{
int[] a = { 1, 2, 3, 4 };
PrintArray(a);
// 1 2 3 4
int[] b = Array.FindAll<int>(a, isOdd);
PrintArray(b);
// 1 3
Array.ForEach<int>(b, PrintSqrt);
// 1
// sqrt(3)
int[] c = Array.ConvertAll<int, int>(a, ToSqr);
PrintArray(c);
// 1 4 9 16
Console.ReadLine();
}
static bool isOdd(int x)
{
return x % 2 == 1;
}
static void PrintArray(int[] x)
{
foreach (int i in x) { Console.Write("{0} ", i); }
Console.WriteLine();
}
static void PrintSqrt(int x)
{
Console.WriteLine(Math.Sqrt(x));
}
static int ToSqr(int x)
{
return x * x;
}
}
抱怨一下,ycul真是cuo,我本地运行得好好的代码加亮js,添加到yculblog模版里面就跑不了了-.-
|
|
|