非通用IEnumerable和通用IEnumerable< T>有什么区别? [英] What is the difference between the non-generic IEnumerable and the generic IEnumerable<T>?

查看:79
本文介绍了非通用IEnumerable和通用IEnumerable< T>有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉遇到这样一个模糊的问题,但是我一直在寻找最好的一天,我读了一篇又一篇的文章(这里有很多问题),但是找不到一个易于理解的答案.

Sorry for such a vague question, but I have been searching around for the best part of a day, I have read article after article (and many questions here) but just cannot find an easy to understand answer.

我(认为我)知道IEnumerable的用途,但是当用通用类型参数定义它时,我只是不明白它的含义,例如:

I (think I) know what IEnumerable is for, but I just can't understand what it means when it is defined with a generic type argument, for example:

IEnumerable<int> test = method();

这只是让我发疯!请让我摆脱苦难,并解释这意味着什么?

This is just driving me mad! Please put me out of misery and explain what it means?

推荐答案

IEnumerable基本上是对象的集合.它具有方法GetEnumerator(),该方法允许您遍历可枚举中的所有对象.

An IEnumerable is basically a collection of objects. It has the method GetEnumerator() which allows you to iterate through all of the objects in the enumerable.

IEnumerable<int>基本上是整数的集合.它具有方法GetEnumerator(),该方法允许您迭代可枚举中的所有整数.

An IEnumerable<int> is basically a collection of integers. It has the method GetEnumerator() which allows you to iterate through all of the integers in the enumerable.

IEnumerable<int> test = method();表示method()正在从某个位置获取整数的集合.它可以是List,数组或其他某种数据类型,但肯定是它们的集合,它们都是整数,并且您可以遍历它们.

IEnumerable<int> test = method(); means that method() is getting a collection if integers from somewhere. It could be a List, an array or some other data type, but it is definitely a group of them and they are all integers, and you have the ability to iterate through them.

这篇文章也可能会有所帮助:是什么IEnumerable和Array,IList和List之间的区别?

This post may be helpful as well: What's the difference between IEnumerable and Array, IList and List?

这篇关于非通用IEnumerable和通用IEnumerable&lt; T&gt;有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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