Using C# 2.0 and 3.0 new features to process a list

2010年2月26日
using System;
using System.Collections.Generic;
namespace RobbieTests
{
    public class ForEachListProc
    {
        public static void Main()
        {
            int[] array = new int[] { 1, 2, 3, 4, 5 };
            object o = new Action<int>(delegate(int x) { Console.WriteLine(x); });
            int[] evennums; /* even numbers */
            int[] oddnums;  /* odd numbers */
 
            Console.WriteLine("array:");
            /* The below use of "delegate" is .NET Framework 2.0 (C# 2.0)
             * syntax */

            Array.ForEach(array, delegate(int x) { Console.WriteLine(x); });
            evennums = Array.FindAll(array, delegate(int x) { return (x % 2 == 0); });
            Console.WriteLine("even numbers:");
            Array.ForEach(evennums, delegate(int x) { Console.WriteLine(x); });
            /* The below use is .NET Framework 3.5 (C# 3.0) syntax */
            oddnums = Array.FindAll(array, x => (x % 2 == 1));
            Console.WriteLine("odd numbers:");
            Array.ForEach(oddnums, delegate(int x) { Console.WriteLine(x); });
            Console.WriteLine(o.GetType());
        }
    }
}

“Using C# 2.0 and 3.0 new features to process a list” 已有 3 条评论

  1. Tony 在

    C#2.0里Array的操作 还支持Delegate这个还真不知道!

  2. 杰 在

    建议多给出些专题方面的讲解,系列讲解就更好了!

  3. Decheng 在

    谢谢二位光顾。其实 C# 3.0 的语言特性我现在还在学习中,所以还没有能力作讲解。待以后有能力了再来写一点专题吧。

留下您的评论