C#中的Ienumerable接口 [英] Ienumerable interface in C#

查看:78
本文介绍了C#中的Ienumerable接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,刚刚学习IEnumerable接口,发现我们可以借助IEnumerable接口迭代通用和非泛型集合,但后来只是为了检查我创建了一个具有一些属性的Customer类。然后在主方法创建了一个该类的数组,添加了几个对象。然后运行foreach循环并且工作正常而不使用IEnumerable Interface.Can你解释,可能是我的问题会很奇怪或愚蠢,但要成为一个无法准确理解什么是IEnumerable以及何时以及为何使用IEnumerable接口。如果有人可以详细解释,请提前感谢。



Hello guys, was just learning about IEnumerable interface and found that we can iterate through generic as well as non-generic collection with the help of IEnumerable interface, but then just to check i created a class Customer with some attributes.Then in Main method created a array of that Class adding few objects.Then ran through foreach loop and worked fine without using IEnumerable Interface.Can u explain, may be my question would be weird or foolish, but to be onest not able to understand exactly what is IEnumerable and when and why to use IEnumerable interface.Thanks in advance if anyone can explain in Detail.

public class Customer1
    {
        private String _Name, _City;
        private long _Mobile;
        private double _Amount;

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public String City
        {
            get { return _City; }
            set { _City = value; }
        }

        public long Mobile
        {
            get { return _Mobile; }
            set { _Mobile = value; }
        }
        
        public double Amount
        {
            get { return _Amount; }
            set { _Amount = value; }
        }
    }
    class IEnumerableTest
    {
        static void Main()
        {
            Customer1[] customers = new Customer1[]
            {
             new Customer1 {Name="Vithal Wadje",City="Mumbai",Mobile=99999999999,Amount=89.45 },
             new Customer1 { Name = "Sudhir Wadje", City = "Latur", Mobile = 8888888888888888888, Amount =426.00 },
             new Customer1 { Name = "Anil", City = "Delhi", Mobile = 7777777777777777777, Amount = 5896.20 }
            };

            foreach (var obj in customers)
            {
                Console.WriteLine(obj.Name + " " + obj.City + " " + obj.Mobile + " " + obj.Amount);
            }
        }
    }

What I have tried:

I have  created a class Customer with some attributes.Then in Main method created a array of that Class adding few objects.Then ran through foreach loop and worked fine without using IEnumerable Interface.

推荐答案

Customer1没有实现IEnumerable,但它没有代码中的那个:

Customer1 does not implement IEnumerable, but it doesn't have to that in code:
Customer1[] customers = new Customer1[] { ... };
foreach (var obj in customers)
   ...

因为所有Array(和其他集合)都实现了IEnumerable。



接口是一份合同,他们说你这样做,你是俱乐部的成员,并获得所有俱乐部特权。 this and that是定义为接口一部分的属性,方法等。

对于IEnumerable,您只需要实现以下方法: />

because all Array (and other collections) do implement IEnumerable.

Interfaces are a contract, they say "it you do this and that, you are a member of the club, and get all club privileges". The "this and that" are the properties, methods, and so forth that are defined to be part of the interface.
In the case of IEnumerable, you just have to implement the following method:

public IEnumerator GetEnumerator()
    {
    ...
    }

并且如何在C#中实现IEnumerable [ ^ ]将告诉你如何做到这一点。

如果你这样做,那么你的班级可以直接用作 foreach collection:

And How to implement IEnumerable in C#[^] will show you how to do that.
If you do, then your class can be directly used as the foreach "collection":

foreach (MyClass mc in MyClassThatImplementsIEnumerable)
   {
   ...
   }


这篇关于C#中的Ienumerable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆