foreach的基本用法及原理——C#

本文介绍C#中使用foreach语句遍历不同类型的集合,包括ArrayList、List<string>、数组及自定义类型Person,并展示了如何为自定义类型实现IEnumerable接口以支持foreach遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace _28_foreach
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList array = new ArrayList() { 1, 2, 3, 4, 5 };
            foreach (object  item in array)
            {
                Console.WriteLine(item);
            }


            Console.WriteLine("===============================");


            List<string> list = new List<string>() { "ABC", "DEF", "GHI" };
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }


            Console.WriteLine("===============================");


            string[] names = { "C#", "VB", "VC" };
            foreach (var item in names )
            {
                Console.WriteLine(item);
            }


            Console.WriteLine("===============================");


            Person p = new Person();
            p[0] = "奥迪";
            p[1] = "奔驰";
            p[2] = "宝马";


            //for (int i = 0; i < p.Count; i++)
            //{
            //    Console.WriteLine(p[i]);
            //}


            //任何类型想使用foreach遍历,就必须在当前类型中存在
            //public IEnumerator GetEnumerator()方法
            //一般通过实现接口来创建该方法
            foreach (string item in p)
            {
                Console.WriteLine(item);

            }


           Console.WriteLine("===============================");



            //内部实现的原理(跟使用foreach实现相同的作用)
            IEnumerator etor = p.GetEnumerator();
            while (etor.MoveNext())
            {
                string str = etor.Current.ToString();
                Console.WriteLine(str);


            }




            Console.ReadKey();
        }
    }


    public class Person:IEnumerable 
    {
        private List<string> listcars = new List<string>();
        //用于返回数量
        public int Count
        {
            get
            {
                return this.listcars.Count;
            }
        }
        public string this[int index]
        {
            get
            {
                return listcars[index];
            }
            set
            {
                if (index >= listcars.Count)
                {
                    listcars.Add(value);
                }
                listcars[index] = value;
            }
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }


        //获取一个对象,而这个对象是用来遍历的
        public IEnumerator GetEnumerator()
        {
            return new PersonEnumerator(listcars); 
        }

    }


    public class PersonEnumerator : IEnumerator 
    {
        //将person中car的集合传进来
        public PersonEnumerator(List<string> _cars)
        {
            cars = _cars;
        }
        public List<string> cars;


        //自己模拟当前对象的索引
        private int index = -1;
        //获取当前正在遍历的对象
        public object Current
        {
            get
            {
                if (index < 0)
                {
                    return null;
                }
                return cars[index]; 
            }
        }


        public bool MoveNext()
        {
            index = index + 1;
            if (index >= cars.Count)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        public void Reset()
        {
            index = -1;
        }
    }
 

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值