简体   繁体   English

IEnumerable<object> a = 新的 IEnumerable<object> (); 我可以这样做吗?<div id="text_translate"><p> 我想创建一个 object IEnumerable&lt;object&gt;的新实例</p><p>我可以这样做吗?</p><pre> IEnumerable&lt;object&gt; a = new IEnumerable&lt;object&gt;();</pre></div></object></object>

[英]IEnumerable<object> a = new IEnumerable<object>(); Can I do this?

I want to create a new instance of an object IEnumerable<object>我想创建一个 object IEnumerable<object>的新实例

Can I do this?我可以这样做吗?

IEnumerable<object> a = new IEnumerable<object>();

You can for example create an instance of List<object> , which implements IEnumerable<object> .例如,您可以创建一个List<object>实例,它实现IEnumerable<object> Example:例子:

List<object> list = new List<object>();
list.Add(1);
list.Add(4);
list.Add(5);

IEnumerable<object> en = list;
CallFunction(en);

Another solution would be to use Empty .另一种解决方案是使用Empty

msdn extract : msdn 摘录

Returns an empty IEnumerable that has the specified type argument.返回具有指定类型参数的空 IEnumerable。

IEnumerable<object> a = Enumerable.Empty<object>();

There is a thread on SO about it: Is it better to use Enumerable.Empty() as opposed to new List to initialize an IEnumerable?有一个关于它的线程: 使用 Enumerable.Empty() 而不是 new List 来初始化 IEnumerable 会更好吗?

If you use an empty array or empty list, those are objects and they are stored in memory.如果您使用空数组或空列表,则它们是对象并且它们存储在内存中。 The Garbage Collector has to take care of them.垃圾收集器必须照顾它们。 If you are dealing with a high throughput application, it could be a noticeable impact.如果您正在处理高吞吐量应用程序,这可能会产生显着影响。

Enumerable.Empty does not create an object per call thus putting less load on the GC. Enumerable.Empty不会在每次调用时创建一个对象,从而减少了 GC 的负载。

Since you now specified you want to add to it, what you want isn't a simple IEnumerable<T> but at least an ICollection<T> .由于您现在指定要添加到其中,因此您想要的不是简单的IEnumerable<T>而是至少一个ICollection<T> I recommend simply using a List<T> like this:我建议简单地使用List<T>像这样:

List<object> myList=new List<object>();
myList.Add(1);
myList.Add(2);
myList.Add(3);

You can use myList everywhere an IEnumerable<object> is expected, since List<object> implements IEnumerable<object> .您可以在任何需要IEnumerable<object>地方使用 myList,因为List<object>实现了IEnumerable<object>

(old answer before clarification) (澄清前的旧答案)

You can't create an instance of IEnumerable<T> since it's a normal interface(It's sometimes possible to specify a default implementation, but that's usually used only with COM).您不能创建IEnumerable<T>的实例,因为它是一个普通接口(有时可以指定默认实现,但通常仅用于 COM)。

So what you really want is instantiate a class that implements the interface IEnumerable<T> .所以,你真正需要的是实例化一个实现接口IEnumerable<T> The behavior varies depending on which class you choose.行为因您选择的类而异。

For an empty sequence use:对于空序列使用:

IEnumerable<object> e0=Enumerable.Empty<object>();

For an non empty enumerable you can use some collection that implements IEnumerable<T> .对于非空枚举,您可以使用一些实现IEnumerable<T>集合。 Common choices are the array T[] , List<T> or if you want immutability ReadOnlyCollection<T> .常见的选择是数组T[]List<T>或者如果你想要不变性ReadOnlyCollection<T>

IEnumerable<object> e1=new object[]{1,2,3};
IEnumerable<object> e2=new List<object>(){1,2,3};
IEnumerable<object> e3=new ReadOnlyCollection(new object[]{1,2,3});

Another common way to implement IEnumerable<T> is the iterator feature introduced in C# 3:另一种实现IEnumerable<T>常用方法是 C# 3 中引入的迭代器功能

IEnumerable<object> MyIterator()
{
  yield return 1;
  yield return 2;
  yield return 3;
}

IEnumerable<object> e4=MyIterator();

No you can't since IEnumerable is an interface.不,你不能,因为IEnumerable是一个接口。

You should be able to create an empty instance of most non-interface types which implement IEnumerable , eg:-您应该能够创建大多数实现IEnumerable非接口类型的空实例,例如:-

IEnumerable<object> a = new object[] { };

or或者

IEnumerable<object> a = new List<object>();

No, You cannot do that.不,你不能那样做。 Use the following line of code instead:请改用以下代码行:

IEnumerable<int> usersIds = new List<int>() {1, 2, 3}.AsEnumerable();

I hope it helps.我希望它有帮助。

The main reason is we can't create object of an interface, and IEnumerable is an interface .主要原因是我们不能创建接口的对象,而 IEnumerable 是一个接口 We need to create object of the class which implements the interface.我们需要创建实现接口的类的对象。 This is the main reason we can't directly create object of IEnumerable.这是我们不能直接创建 IEnumerable 对象的主要原因。

I wanted to create a new enumerable object or list and be able to add to it.我想创建一个新的可枚举对象或列表并能够添加到它。

This comment changes everything.这条评论改变了一切。 You can't add to a generic IEnumerable<T> .您不能添加到通用IEnumerable<T> If you want to stay with the interfaces in System.Collections.Generic , you need to use a class that implements ICollection<T> like List<T> .如果您想继续使用System.Collections.Generic的接口,则需要使用实现ICollection<T>的类,如List<T>

You can do this:你可以这样做:

IEnumerable<object> list = new List<object>(){1, 4, 5}.AsEnumerable();
CallFunction(list);

No IEnumerable is an interface, you can't create instance of interface没有 IEnumerable 是一个接口,你不能创建接口的实例

you can do something like this你可以做这样的事情

IEnumerable<object> a = new object[0];

It's a pretty old question, but for the sake of newcomers, this is how we can protect an IEnumerable<T> from a null exception .这是一个很老的问题,但对于新手来说,这就是我们如何保护IEnumerable<T>免受null exception Another word, to create an empty instance of a variable of type IEnumerable<T>换句话说,创建一个IEnumerable<T>类型变量的空实例

public IEnumerable<T> MyPropertyName { get; set; } = Enumerable.Empty<T>();

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.empty?view=net-5.0 https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.empty?view=net-5.0

Cheers.干杯。

I have been Implementing IEnumerable of type IEnumerable<T> by this way我一直在通过这种方式实现IEnumerable IEnumerable<T>类型的 IEnumerable

IEnumerable<T> MyEnum = new T[]{ /*object of type T*/ };

Example:例子:

var Object = new Book{id = 1,name = "Hello World"};
IEnumerable<Book> MyEnum = new Book[]{ Object };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IEnumerable对象? - IEnumerable to Object? 从IEnumerable转换为IEnumerable <object> - Cast from IEnumerable to IEnumerable<object> 检查IEnumerable <object>是否为IEnumerable <int> - Check if IEnumerable<object> is IEnumerable<int> 为什么`IEnumerable<int> ` 不是 `IEnumerable<object> `?<div id="text_translate"><p> IEnumerable<T>接口是协变的,因此IEnumerable<string>是IEnumerable<object> 。 但是为什么IEnumerable<int><em><strong>不是</strong></em>IEnumerable<object>而int是object ?</p></div></object></int> - Why `IEnumerable<int>` is not `IEnumerable<object>`? 如果Ienumerable List返回1个对象,我可以将其转换为对象吗? - If Ienumerable List returns 1 object can I convert it to an object? 将对象转换为IEnumerable <object> ? - Cast object to IEnumerable<object>? 如何获取IEnumerable上通用参数的类型 <object> ? - How do I get the type of a generic parameter on IEnumerable<object>? C# 我可以用 IEnumerable 对象和结构实现接口吗? - C# Can i implement interface with IEnumerable object and struct? 当它是 IEnumerable 时,我如何枚举类型为“T”的 object - How can I enumerate through an object of type `T` when it is a IEnumerable 为什么我可以将对象与这些对象的IEnumerable进行比较而不是List? - Why can I compare an object to an IEnumerable of those objects but not a List?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM