C#新特性

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

    #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

你可能感兴趣的:(C#新特性)