为什么 IList<>.Reverse() 不像 List<>().Reverse 那样工作

发布于 2024-10-11 16:29:07 字数 949 浏览 3 评论 0 原文

我对 List.Reverse()Reverse(this IEnumerable source) 有问题。 看代码:

  // Part 1
  List<int> list = new List<int> { 1, 2, 3 };

  foreach (int x in list)
    Console.Write(x);

  Console.WriteLine();

  list.Reverse();

  foreach (int x in list)
    Console.Write(x);

  Console.WriteLine();
  list.Reverse();

  // Part2
  IList<int> ilist = list;

  foreach (int x in list)
    Console.Write(x);      

  Console.WriteLine();

  ilist.Reverse();

  foreach (int x in ilist)
    Console.Write(x);

  Console.WriteLine();

  ilist.Reverse();

我的结果:

123
321
123
123

因为Reverse()-Part1是List.Reverse()Reverse()-Part2是 Reverse(此 IEnumerable 源) 我想在第 2 部分中为 IList 执行 List.Reverse()。我怎样才能做到呢?

I have problem with List<T>.Reverse() and Reverse(this IEnumerable<TSource> source).
Look to the code:

  // Part 1
  List<int> list = new List<int> { 1, 2, 3 };

  foreach (int x in list)
    Console.Write(x);

  Console.WriteLine();

  list.Reverse();

  foreach (int x in list)
    Console.Write(x);

  Console.WriteLine();
  list.Reverse();

  // Part2
  IList<int> ilist = list;

  foreach (int x in list)
    Console.Write(x);      

  Console.WriteLine();

  ilist.Reverse();

  foreach (int x in ilist)
    Console.Write(x);

  Console.WriteLine();

  ilist.Reverse();

My result:

123
321
123
123

because Reverse()-Part1 is List<T>.Reverse(), Reverse()-Part2 is Reverse(this IEnumerable<TSource> source)
I want execute List<int>.Reverse() in Part2 for IList<int>. How I can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-10-18 16:29:08

IList 没有 Reverse 方法,因此它使用扩展方法。在 IList 引用上使用 List.Reverse 的唯一方法是将其强制转换或转换为 List >。仅当您首先确定它确实是一个 List 时,转换才会起作用:

IList<int> abstractList;
var concreteList = (List<int>)abstractList;
concreteList.Reverse();

另一种选择是创建一个 List 来自您的 IList 实例,而不是假设它已经是 List

IList<int> abstractList;
var concreteList = new List<int>(abstractList);
concreteList.Reverse();

原因 code>Reverse 扩展方法实际上并不影响底层列表,因为它在 IEnumerable 上运行,而该列表不一定是可写的(Enumerable 都不是) code> 扩展方法对原始集合进行更改,它们返回一个集合)。

要使用此版本的 Reverse,只需使用 Reverse 调用的乘积,而不是原始列表:

IList<int> abstractList;
IEnumerable<int> reversedList = abstractList.Reverse();

IList<int> doesn't have a Reverse method, so it uses the extension method. The only way to use List<T>.Reverse on your IList<int> reference is to cast or convert it to a List<int>. Casting will only work if you're sure that it's really a List<int> in the first place:

IList<int> abstractList;
var concreteList = (List<int>)abstractList;
concreteList.Reverse();

Another option would be to create a List<int> from your IList<int> instance, rather than assuming it already is a List<int>:

IList<int> abstractList;
var concreteList = new List<int>(abstractList);
concreteList.Reverse();

The reason that the Reverse extension method doesn't actually affect the underlying list is because it operates on IEnumerable<T>, which isn't necessarily writeable (none of the Enumerable extension methods make changes to the original collection, they return a new collection).

To use this version of Reverse, just use the product of the Reverse call, rather than the original list:

IList<int> abstractList;
IEnumerable<int> reversedList = abstractList.Reverse();
も星光 2024-10-18 16:29:08

在第二个示例中,您对 IEnumerable 使用扩展方法,这不会改变原始集合,而是生成一个查询,该查询将导致原始列表的顺序相反命令。也就是说,如果你想利用ilist.Reverse()的结果,你会说

var reversedList = iList.Reverse(); 
// reversedList is IEnumerable<int>
// 'list' and 'ilist' are not changed

foreach (int item in reversedList)
    Console.Write(item);

In the second example, you're using an extension method against IEnumerable<T>, and this is not mutating the original collection but rather producing a query that would result in a sequence of your original list in reverse order. That is to say, if you want to utilize the results of ilist.Reverse(), you would say

var reversedList = iList.Reverse(); 
// reversedList is IEnumerable<int>
// 'list' and 'ilist' are not changed

foreach (int item in reversedList)
    Console.Write(item);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文