List的FindIndex和ForEach

FindIndex和ForEach能接受一个delegate,用来定义查找规则太好用。

匿名函数,萌萌哒;Conditional("DEBUG")也是极好的。

读《C#本质论》

List的FindIndex和ForEach
using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Diagnostics;



namespace WindowsFormsApplication1

{

    static class Program

    {



        class CADFileItem

        {

            public string GUID;

            public string Name;

            public string ModelClass;

            public string ModelType;

            public CADFileItem()

            {

                this.GUID = "0000-0000-0000-0000";

                this.Name = "NoName";

                this.ModelClass = "Aseembly";

                this.ModelType = "FamilyMain";

            }

            [Conditional("DEBUG")]

            public void WriteLine()

            {

                Debug.WriteLine(string.Format(@"GUID: {0}

Name: {1}

ModelClass: {2}

ModelType: {3}", GUID, Name, ModelClass, ModelType));

            }

        }

        class FindCADFile

        {

            string findValue;

            public FindCADFile(string val)

            {

                findValue = val;

            }



            public bool FindGUID(CADFileItem file)

            {

                return file.GUID == findValue;

            }



            public bool FindName(CADFileItem file)

            {

                return file.Name == findValue;

            }



            public bool FindModelClass(CADFileItem file)

            {

                return file.ModelClass == findValue;

            }

        }



        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {



            List<CADFileItem> files = new List<CADFileItem>();

            int intCount = 5;

            for (int u = 0; u < intCount; ++u)

            {

                CADFileItem file = new CADFileItem();

                file.GUID = file.GUID.Replace("-0000", "-000" + u);

                file.Name = "CADFile" + u;

                if(u==0 || u==intCount-1)

                    file.ModelClass = "Part";

                else

                    file.ModelClass = "Assembly";

                file.WriteLine();

                files.Add(file);

            }





            int ret = files.FindIndex(new FindCADFile("0000-0002-0002-0002").FindGUID);

            if (ret != -1)

            {

                Debug.WriteLine("FindGUID--------");

                files[ret].WriteLine();

            } 

            ret = files.FindIndex(new FindCADFile("CADFile3").FindName);

            if (ret != -1)

            {

                Debug.WriteLine("FindName--------");

                files[ret].WriteLine();

            }

            ret = files.FindIndex(new FindCADFile("Part").FindModelClass);

            if (ret != -1)

            {

                Debug.WriteLine("FindModelClass--------");

                files[ret].WriteLine();

            }

            

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

        }

    }

}
View Code

 

更多:http://msdn.microsoft.com/zh-cn/library/x1xzf2ca(v=vs.110).aspx

你可能感兴趣的:(foreach)