C#新特性

    技术2022-05-11  93

    这篇文章只是学习 迭代器 和匿名方法 的一些实例代码,并没有什么技术含量:

        #region 迭代器

        public class SampleCollection    {        public object[] array;

            public SampleCollection(object[] oArray)        {            array = oArray;        }

            public System.Collections.IEnumerable BuildCollection()        {            for (int i = 0; i < array.Length; i++)            {                yield return array[i];            }        }    }

        public class UseCollection    {        public void GetSingleItem()        {            object[] oarray = new object[] { "a","b"};            SampleCollection cole = new SampleCollection(oarray);            string strColl = "";            foreach (object osin in cole.BuildCollection())            {                strColl += osin.ToString();            }        }    }

        #endregion

        #region 匿名方法

        public class SubClass    {        delegate void SomeDelete();

            public void InvokMethod()        {            SomeDelete de = delegate()            {                //方法体                string str = "1";                string str1 = str;            };                        de();        }

            //常规调用        void Method()        {            //方法体            string str = "1";            string str1 = str;        }        public void InvokeMe()        {            SomeDelete sun = new SomeDelete(Method);            sun();        }    }    #endregion


    最新回复(0)