当前位置: 首页 > news >正文

做网站广告的点/网站优化哪家好

做网站广告的点,网站优化哪家好,长沙做网站的包吃包住4000,建网站 南京枚举 在foreach语句中使用枚举,可以迭代集合中的元素,且无需知道集合中的元素个数. 数组或集合实现带GetEumerator()方法的IEumerable接口.GetEumerator()方法返回一个实现IEunmerable接口的枚举. GetEnumerator()方法用IEnumerable接口定义.foreach语句并不真的需要在集合类中…

枚举

foreach语句中使用枚举,可以迭代集合中的元素,且无需知道集合中的元素个数.

数组或集合实现带GetEumerator()方法的IEumerable接口.GetEumerator()方法返回一个实现IEunmerable接口的枚举.

GetEnumerator()方法用IEnumerable接口定义.foreach语句并不真的需要在集合类中实现这个借口.有一个名为GetEnumerator()的方法,他返回实现了IEnumerator接口的对象就足够了.

 

IEnumerator接口

foreach语句使用IEnumerator接口的方法和属性,迭代集合中的所有元素.为此IEnumerator定义了Current属性,来返回光标所在的元素,该接口的MoveNext()方法移动到集合的下一个元素上,如果有这个元素,该方法就返回true.如果集合不再有更多的元素,该方法就返回false.

这个借口的泛型版本IEnumerator<T>派生自接口IDisposable,因此定义了Dispose()方法,来清理枚举器占用的资源.

 

foreach语句

C#foreach语句不会解释为IL代码中的foreach语句.C#编译器会把foreach语句转换为IEnumerable接口的方法和属性.案例:

int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };

foreach (var item in arr)

{

     Console.WriteLine(item);

}

很明显,foreach语句很简洁,但是他的有点不仅仅在于此,它的效率也是很高的,不用考虑数组是几维的.案例:

int[,] array = new int[8, 8];

            for (int i = 0; i < array.GetLength(0); i++)

            {

                for (int j = 0; j < array.GetLength(1); j++)

                {

                    Console.WriteLine(array[i,j].ToString());

                }

            }

            Console.ReadKey();

使用foreach:

foreach (int item in array)

            {

                Console.WriteLine(item.ToString());

            }

对于三维或者更多维,foreach语句不用发生任何变化,而对于for语句就要进行修改了.

 

foreach完成类型转换操作,案例:

int[] array = new int[100];

            ArrayList aList = new ArrayList();

            aList.AddRange(array);

            foreach (int item in aList)

            {

                Console.WriteLine(item.ToString());

            }

            for (int i = 0; i < aList.Count; i++)

            {

                int n = (int)aList[i];

                Console.WriteLine(n.ToString());

            }

            Console.ReadKey();

 

foreach并没有增加资源使用,由于对于继承了IEnumerable接口的数据类型,才能使用foreach语句,那么对于使用foreach会访问IEnumerable接口中的GetEnumerator()方法来进行枚举,那么对应如上的foreach语句,对应的语句如下:

 IEnumerator it = aList.GetEnumerator() as IEnumerator;

            using (IDisposable disp = it as IDisposable)

            {

                while (it.MoveNext())

                {

                    int elem = (int)it.Current;

                    Console.WriteLine(elem.ToString());

                }

            }

也即是说再出了foreach语句之后对于IEnumerator的对象也进行IDispose处理.

 

foreach的两种限制

不能修改枚举成员:

int[] array = new int[100];

            foreach (var item in array)

            {

                item++;//这是错误的,因为枚举成员是只读的

                Console.WriteLine(item.ToString());

            }

不要对集合进项删除操作:

int[] array = new int[100];

            ArrayList alist = new ArrayList();

            alist.AddRange(array);

            foreach (var item in alist)

            {

                alist.Remove(item);//这是错误的

                Console.WriteLine(item.ToString());

            }

对于删除成员和修改成员可以使用for循环来处理,对于一个记录集的多条数据删除问题,也是经常出现的问题,由于在一些记录集中进行删除的时候,在删除操作之后相应的索引也发生了变化,这时候的删除要反过来进行删除:

int[] array = new int[100];

            ArrayList alist = new ArrayList();

            alist.AddRange(array);

            for (int i = alist.Count-1; i >=0; i--)

            {

                int n = (int)alist[i];

                if (n==5)

                {

                    alist.RemoveAt(i);

                }

                Console.WriteLine(n.ToString());

            }

 

除了上述提到的foreach的两个约束外,foreach可以用于人和循环.

 

yield语句

C#中的yield语句便于创建枚举器.

yield语句的两种形式:

1.yield return <expression>

2.yield break;

 

使用一个yield return语句返回集合的一个元素

包含yield语句的方法或属性是迭代器.迭代器必须满足下列要求:

a.返回类型必须是IEnumerable,IEnumerable<T>,IEnumeratorIEnumerator<T>

b.他不能有任何refout参数.

 

yield return语句不能位于try-catch;yield return语句可以位于try-finallytry

yield return语句返回集合的一个元素,并移动到下一个元素上.yield break可以停止迭代.

 

class Program

    {

        static void Main(string[] args)

        {

            HelloCollection hello = new HelloCollection();

            foreach(string item in hello)

            {

                Console.WriteLine(item);                 

            }

            Console.ReadKey();   

        }

 

    }

    public class HelloCollection

    {

        public  IEnumerator<string> GetEnumerator()

        {

            //yield return语句返回集合的一个元素,并移动到下一个元素上

            //yield break可以终止迭代

            yield return "hello";

            yield return "world";

        }

}

 

使用yield return语句实现以不同方式迭代集合的类:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace 枚举

{

    class Program

    {

        static void Main(string[] args)

        {

            MusicTitles music = new MusicTitles();

            foreach(var item in music.GetEnumerator())

            {

                Console.WriteLine(item);

            }

            Console.WriteLine();

            foreach (string item in music.Reverse())

            {

                Console.WriteLine(item);

            }

            Console.WriteLine();

            foreach (var item in music.Subset(2,2))

            {

                Console.WriteLine(item);

            }

            Console.ReadKey();   

        }

 

    }

    public class MusicTitles

    {

        string[] names = {"a","b","c","d" };

        public IEnumerable<string> GetEnumerator()

        {

            foreach (string item in names)

            {

                yield return item;

            }

        }

        public IEnumerable<string> Reverse()

        {

            for (int i = 3; i >=0; i--)

            {

                yield return names[i];

            }

        }

        public IEnumerable<string> Subset(int index, int offert)

        {

            for (int i = index; i < index+offert; i++)

            {

                yield return names[i];

            }

        }

    }

}

 

转载于:https://www.cnblogs.com/android-blogs/p/6598456.html

http://www.lbrq.cn/news/1112941.html

相关文章:

  • 个人可以做网站/图片外链生成工具
  • 做任务刷单的网站是真的吗/什么是网络推广工作
  • 免费制造网站/兰州seo培训
  • 网站开发+.net+开源/佛山旺道seo
  • 南宁百度seo网站优化/西安竞价托管
  • 网站聊天代码/谷粉搜索谷歌搜索
  • asp网站 被插入/全国疫情一览表
  • 崇信县门户网站官网/怎么做电商
  • 公司软件网站建设/请简述网络营销的特点
  • 京津冀协同发展现状/seo人员招聘
  • 用jq和ajax做能登陆注册的一个网站/网络服务器的功能
  • 做地方旅游网站目的意义/成都seo外包
  • 平面设计网站排行榜/怎么宣传自己的产品
  • 红色企业网站/湛江seo
  • 南通百度网站快速优化/互联网推广引流公司
  • 王爷不要漫画/seo工具优化软件
  • 网展企业网站系统 免费/商品推广与营销的方式
  • 常德市做网站联系电话/免费建站免费推广的网站
  • 武昌网站制作建设/国内seo服务商
  • 上海哪家公司提供专业的网站建设/百度pc端提升排名
  • 外汇网站建设制作/抖音网络营销案例分析
  • 行业网站名录/企业推广策划公司
  • 东莞网站制作支付通道/自动引流免费app
  • 做外贸网站卖什么东西好/b2b网站平台
  • 门户网站建设注意问题/外贸网站平台有哪些
  • 温州网站提升排名/百度关键词排名联系方式
  • 常熟公司网站建设电话/搜索引擎优化的技巧
  • 北京网站建设优化学校/全国疫情高峰感染高峰进度查询
  • 企业建站系统营销吧tt团队/百度网盘电脑版
  • wordpress域名重复/seo服务优化
  • JS - - - - - 数组乱序排序「进阶版」
  • 自动化技术在造纸行业的应用:EtherCAT转PROFIBUS DP解决方案
  • 零基础学Vue3组件化开发
  • 在UE中如何操作视图的大小,方位,移动
  • [Linux入门] Linux 账号和权限管理入门:从基础到实践
  • 有哪些好用的原型设计软件?墨刀、Axure等测评对比