如何不能隐含类型ıenumeration列出字符串 [英] How to cannot implicity type ıenumeration to list string

查看:97
本文介绍了如何不能隐含类型ıenumeration列出字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的IEnumerable<串GT; t;

公开列表< string> folder = new List< string>();



Folder = t;



Send.FSend(路径,T,错误); // t是错误,不能隐含错误。



如何解决这个问题。



我尝试过:



Ienumerable<string> t;
Public List<string> folder=new List<string>();

Folder=t;

Send.FSend(path,t,"error"); // t is error and cannot implicity error.

How to solve this problem.

What I have tried:

Ienumerable<string> t;
Public List<string> folder=new List<string>();

Folder=t;

Send.FSend(path,t,"error"); // t is error and cannot implicity error

推荐答案

t IEnumerable< T> ,而不是列表< T>

文件夹只能包含作为IEnumerable超集的列表。



你可以反过来这样做:

t is an IEnumerable<T>, not a List<T>
And folder can only contain Lists which are a "superset" of IEnumerable.

You can do it the other way around:
IEnumerable<string> t;
List<string> folder = new List<string>();
t = folder;

因为每个List都是IEnumerable - 但你不能这样做。



想一想:如果可以,会发生什么?当您尝试使用List时,List特定功能(索引,添加和删除项目等)将不存在,因此代码将失败。



您可以使用Linq方法将IEnumerable转换为List:

Because every List is IEnumerable - but you can't do it the other way.

Think about it: what would happen if you could? When you tried to use the List, the List specific features (indexing, adding and removing items, etc.) would not be there, so teh code would fail.

You can do it by using a Linq method to convert the IEnumerable to a List:

IEnumerable<string> t = ...;
List<string> folder = t.ToList();

但是这会创建一个新对象(一个List),它包含与原始IEnumerable相同的项目,但是没有附加它,所以更改为一个将不影响对方。

But that creates a new object (a List) which contains the same items as the original IEnumerable, but which is not "attached" to it, so changes to one will not affect the other.


这篇关于如何不能隐含类型ıenumeration列出字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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