C#_IEnumerable和IEnumerable<T>

C# 实现非泛型和泛型可枚举接口
本文介绍了如何在C#中实现非泛型和泛型的IEnumerable接口,通过实例展示了如何定义类并实现可枚举接口,以及如何使用foreach循环遍历实现。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CShapeTest
{
    // 实现非泛型IEnumerable可枚举接口
    class Teacher:IEnumerable
    {
        private List<Student> stuList;

        public Teacher()
        {
            stuList = new List<Student>();
        }

        public void add(Student stu)
        {
            this.stuList.Add(stu);
        }

        public IEnumerator GetEnumerator()
        {
            return this.stuList.GetEnumerator();
        }
    }

    class Student
    {
        private int age;
        private string name;

        public Student(int age, string name)
        {
            this.age = age;
            this.name = name;
        }

        public override string ToString()
        {
            return string.Format("age:{0}, name:{1}", this.age, this.name);
        }
    }

    class Start
    {
        static void Main(string[] args)
        {
            Teacher tea = new Teacher();
            tea.add(new Student(20, "cxm"));
            tea.add(new Student(23, "zqr"));
            tea.add(new Student(25, "chenglong"));
            tea.add(new Student(27, "xiaoming"));
            tea.add(new Student(30, "lisi"));

            foreach (var item in tea)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CShapeTest
{
    // 实现泛型IEnumerable<T>可枚举接口
    class Teacher:IEnumerable<Student>
    {
        private List<Student> stuList;

        public Teacher()
        {
            stuList = new List<Student>();
        }

        public void add(Student stu)
        {
            this.stuList.Add(stu);
        }

        //可枚举的类可以返回枚举  
        public IEnumerator<Student> GetEnumerator()
        {
            foreach (Student s in stuList)
            {
                yield return s;
            }
        } 

        //显式实现接口  
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        } 
    }

    class Student
    {
        private int age;
        private string name;

        public Student(int age, string name)
        {
            this.age = age;
            this.name = name;
        }

        public override string ToString()
        {
            return string.Format("age:{0}, name:{1}", this.age, this.name);
        }
    }

    class Start
    {
        static void Main(string[] args)
        {
            Teacher tea = new Teacher();
            tea.add(new Student(20, "cxm"));
            tea.add(new Student(23, "zqr"));
            tea.add(new Student(25, "chenglong"));
            tea.add(new Student(27, "xiaoming"));
            tea.add(new Student(30, "lisi"));

            foreach (var item in tea)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
    }
}


public void AsyncUploadBills<T>(List<T> bills) where T : class, new() { UserEntity userEntity = LoginUserManager.Instance.UserEntity; List<T> ts1 = new List<T>(); List<T> ts2 = new List<T>(); bills.ForEach((T bill) => { try { List<BillCommodityEntity> billCommodityEntities = ((object)bill as BillEntity).commoditys; if (billCommodityEntities != null) { List<BillCommodityEntity> billCommodityEntities1 = billCommodityEntities; Action<BillCommodityEntity> u003cu003e9_21 = BasicModule.<>c__2<T>.<>9__2_1; if (u003cu003e9_21 == null) { u003cu003e9_21 = (BillCommodityEntity i) => { string str = i.promotioninfo; if (!string.IsNullOrEmpty(str) && str != "null") { i.promotioninfo = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<TempPromotionInfoEntity>(str)); } string str1 = i.couponinfo; if (!string.IsNullOrEmpty(str1) && str1 != "null") { i.couponinfo = JsonConvert.SerializeObject(JsonConvert.DeserializeObject<TempCouponInfoEntity>(str1)); } }; BasicModule.<>c__2<T>.<>9__2_1 = u003cu003e9_21; } billCommodityEntities1.ForEach(u003cu003e9_21); } Dictionary<string, string> strs = new Dictionary<string, string>() { { "access_code", userEntity.access_code } }; List<T> ts = new List<T>() { bill }; strs.Add("salebills", Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(ts)))); strs.Add("branchcode", userEntity.branchcode); strs.Add("deviceuniquecode", LoginUserManager.Instance.DeviceUniqueCode); string str2 = (new Responser()).RequestDataPost(string.Concat(EnvironmenCache.Instance.DeviceUniqueCode, "/mt/pos/retail/saveSaleBills"), strs); object obj = JsonConvert.DeserializeObject(str2); if (BasicModule.<>o__2<T>.<>p__3 == null) { BasicModule.<>o__2<T>.<>p__3 = CallSite<Func<CallSite, object, bool>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.UnaryOperation(CSharpBinderFlags.None, ExpressionType.IsTrue, typeof(BasicModule), (IEnumerable<CSharpArgumentInfo>)(new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }))); } !0 target = BasicModule.<>o__2<T>.<>p__3.Target; CallSite<Func<CallSite, object, bool>> u003cu003ep_3 = BasicModule.<>o__2<T>.<>p__3; if (BasicModule.<>o__2<T>.<>p__2 == null) { BasicModule.<>o__2<T>.<>p__2 = CallSite<Func<CallSite, string, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "Equals", null, typeof(BasicModule), (IEnumerable<CSharpArgumentInfo>)(new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.Constant, null), CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }))); } !0 _u00210 = BasicModule.<>o__2<T>.<>p__2.Target; CallSite<Func<CallSite, string, object, object>> u003cu003ep_2 = BasicModule.<>o__2<T>.<>p__2; if (BasicModule.<>o__2<T>.<>p__1 == null) { BasicModule.<>o__2<T>.<>p__1 = CallSite<Func<CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(CSharpBinderFlags.None, "ToString", null, typeof(BasicModule), (IEnumerable<CSharpArgumentInfo>)(new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }))); } !0 target1 = BasicModule.<>o__2<T>.<>p__1.Target; CallSite<Func<CallSite, object, object>> u003cu003ep_1 = BasicModule.<>o__2<T>.<>p__1; if (BasicModule.<>o__2<T>.<>p__0 == null) { BasicModule.<>o__2<T>.<>p__0 = CallSite<Func<CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(CSharpBinderFlags.None, "success", typeof(BasicModule), (IEnumerable<CSharpArgumentInfo>)(new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }))); } if (!target(u003cu003ep_3, _u00210(u003cu003ep_2, "1", target1(u003cu003ep_1, BasicModule.<>o__2<T>.<>p__0.Target(BasicModule.<>o__2<T>.<>p__0, obj))))) { ((object)bill as BillEntity).ticketstatue = "failed"; ts1.Add(bill); } else { ((object)bill as BillEntity).ticketstatue = "updated"; ts2.Add(bill); } BasicModule.logger.Info(string.Concat(((object)bill as BillEntity).ticketcode, " saveSaleBills json:", str2)); } catch (Exception exception) { BasicModule.logger.Error("订单上传失败", exception); ((object)bill as BillEntity).ticketstatue = "failed"; ts1.Add(bill); } }); try { if (ts1.Any<T>()) { using (SqlSugarClient instance = SugarDao.Instance) { instance.Updateable<T>(ts1).ExecuteCommand(); } } if (ts2.Any<T>()) { using (SqlSugarClient sqlSugarClient = SugarDao.Instance) { sqlSugarClient.Updateable<T>(ts2).ExecuteCommand(); } } } catch (Exception exception1) { BasicModule.logger.Error("订单上传状态分拣失败", exception1); } }
最新发布
07-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值