C# ArrayList和IEnumerable查询
为什么ArrayList继承IEnumerable格式时,以下DisplayContents不起作用(不会编译)C# ArrayList和IEnumerable查询,c#,C#,为什么ArrayList继承IEnumerable格式时,以下DisplayContents不起作用(不会编译) 公共类程序 { 静态void Main(字符串[]参数) { 列表l=新列表(){1,2,3}; 显示内容(l); 字符串[]l2=新字符串[]{“ss”,“ee”}; 显示内容(l2); ArrayList l3=新的ArrayList(){“ss”,“ee”}; 显示内容(l3); Console.ReadLine(); } 公共静态void显示内容(IEnumerable集合)
公共类程序
{
静态void Main(字符串[]参数)
{
列表l=新列表(){1,2,3};
显示内容(l);
字符串[]l2=新字符串[]{“ss”,“ee”};
显示内容(l2);
ArrayList l3=新的ArrayList(){“ss”,“ee”};
显示内容(l3);
Console.ReadLine();
}
公共静态void显示内容(IEnumerable集合)
{
foreach(集合中的var\u项)
{
控制台写入线(_项);
}
}
}
ArrayList
实现了IEnumerable
,但不是泛型IEnumerable
更新:
这将有助于:
public static void DisplayContents(IEnumerable collection)
{
foreach (var _item in collection)
Console.WriteLine(_item);
}
ArrayList
实现IEnumerable
,但不是泛型IEnumerable
更新:
这将有助于:
public static void DisplayContents(IEnumerable collection)
{
foreach (var _item in collection)
Console.WriteLine(_item);
}
好的,快速检查一下告诉我,ArrayList
并没有实现IEnumerable
,而是实现了IEnumerable
,这是有道理的,因为ArrayList
是泛型之前的遗留产物,今天几乎没有真正的用途
根本没有理由使用ArrayList
。您至少可以使用列表
,但这能解决什么问题?除非您绝对需要一组随机类型,这些随机类型不能/不能实现公共接口,也不能分组为新类型,否则请使用更具体的泛型参数。好的,快速检查一下告诉我,ArrayList
不能实现IEnumerable
,但实际上实现了IEnumerable
,这是有道理的,因为ArrayList
是泛型出现之前遗留下来的产物,今天几乎没有实际用途
根本没有理由使用
ArrayList
。您至少可以使用列表
,但这能解决什么问题?除非您绝对需要一组随机类型,这些随机类型没有/无法实现公共接口,并且无法分组为新类型,否则请使用更具体的泛型参数。ArrayList
实现IEnumerable
,而不是泛型IEnumerable
。这是意料之中的,因为ArrayList
既不是泛型,也不绑定到任何特定类型
您需要将DisplayContents
方法的参数类型从IEnumerable
更改为IEnumerable
,并删除其类型参数。集合中的项目将传递到控制台。WriteLine可以接受任何对象
public static void DisplayContents(IEnumerable collection)
{
foreach (var _item in collection)
{
Console.WriteLine(_item);
}
}
ArrayList
实现IEnumerable
,但不实现泛型IEnumerable
。这是意料之中的,因为ArrayList
既不是泛型,也不绑定到任何特定类型
您需要将DisplayContents
方法的参数类型从IEnumerable
更改为IEnumerable
,并删除其类型参数。集合中的项目将传递到控制台。WriteLine可以接受任何对象
public static void DisplayContents(IEnumerable collection)
{
foreach (var _item in collection)
{
Console.WriteLine(_item);
}
}
如果您将呼叫线路更改为此,它将工作:
DisplayContents(l3.ToArray());
如果您将呼叫线路更改为此,它将工作:
DisplayContents(l3.ToArray());
这将起作用,因为string[]
实现了IEnumerable
这将起作用,因为string[]
实现了IEnumerable
扩展方法如何
/// <summary>
/// Projects any <see cref="IEnumerable"/>, e.g. an <see cref="ArrayList"/>
/// to an generic <see cref="IEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The type to project to.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="map">The mapping function.</param>
/// <returns>A sequence of <typeparamref name="T"/>.</returns>
public static IEnumerable<T> Select<T>(this IEnumerable source, Func<object, T> map)
{
foreach(var item in source)
{
yield return map(item);
}
}
//
///任何项目,例如
///到一个通用的。
///
///要投影到的类型。
///源序列。
///映射函数。
///一连串的。
公共静态IEnumerable选择(此IEnumerable源,函数映射)
{
foreach(源中的var项)
{
收益回报图(项目);
}
}
扩展方法如何
/// <summary>
/// Projects any <see cref="IEnumerable"/>, e.g. an <see cref="ArrayList"/>
/// to an generic <see cref="IEnumerable{T}"/>.
/// </summary>
/// <typeparam name="T">The type to project to.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="map">The mapping function.</param>
/// <returns>A sequence of <typeparamref name="T"/>.</returns>
public static IEnumerable<T> Select<T>(this IEnumerable source, Func<object, T> map)
{
foreach(var item in source)
{
yield return map(item);
}
}
//
///任何项目,例如
///到一个通用的。
///
///要投影到的类型。
///源序列。
///映射函数。
///一连串的。
公共静态IEnumerable选择(此IEnumerable源,函数映射)
{
foreach(源中的var项)
{
收益回报图(项目);
}
}
您遇到了什么样的错误?抱歉的错误是“参数1:无法从'System.Collections.ArrayList'转换为'System.Collections.Generic.IEnumerable'”。您遇到了什么样的错误?抱歉的错误是“参数1:无法从'System.Collections.ArrayList'转换为'System.Collections.Generic.IEnumerable'”,或者更好,根本不要使用ArrayList
,因为它是不必要的。如果您确实需要,请使用List
,答案可能是“否”。那么它对数组是如何工作的呢?i、 e.它适用于字符串[]@Noel:array为其特定类型实现IEnumerable
ArrayList
是一种过时的类型,当引入泛型时,没有理由向它添加另一个接口。string[]
实现了IEnumerable
(以及ICollection
和IList
)。@Douglas:我看到的唯一优势是它实现了IEnumerable
。),我就是这么说的;只有在您无法控制的API强制使用ArrayList时,您才会/应该使用ArrayList。或者更好的是,不要使用ArrayList
,因为它是不必要的。如果您确实需要,请使用List
,答案可能是“否”。那么它对数组是如何工作的呢?i、 e.它适用于字符串[]@Noel:array为其特定类型实现IEnumerable
ArrayList
是一种过时的类型,引入泛型时没有理由向它添加另一个接口。string[]
实现了IEnumerable
(以及ICollection
和IList
)。@Douglas:我看到的唯一优势是它实现了