ArraySegment< T>的用途是什么?类? [英] What is the use of the ArraySegment<T> class?

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

问题描述

在将 MessageEncoder 类子类化时,我碰到了 ArraySegment< byte> 类型。

I just came across the ArraySegment<byte> type while subclassing the MessageEncoder class.

我现在了解到它是给定数组的一部分,需要偏移量,不可枚举,并且没有索引器,但是我仍然无法理解它的用法。有人可以举例说明吗?

I now understand that it's a segment of a given array, takes an offset, is not enumerable, and does not have an indexer, but I still fail to understand its usage. Can someone please explain with an example?

推荐答案

ArraySegment< T> .NET 4.5中变得更加有用了 +和。 NET Core 现已实现:


  • IList< T>

  • ICollection< T>

  • IEnumerable< T>

  • IEnumerable

  • IReadOnlyList< T>

  • IReadOnlyCollection< T>

  • IList<T>
  • ICollection<T>
  • IEnumerable<T>
  • IEnumerable
  • IReadOnlyList<T>
  • IReadOnlyCollection<T>

,而不是 .NET 4版本,该版本完全没有实现任何接口。

as opposed to the .NET 4 version which implemented no interfaces whatsoever.

该类现在可以参加该活动了LINQ的美好世界,因此我们可以执行常见的LINQ事情,例如查询内容,在不影响原始数组的情况下反转内容,获取第一项,等等:

The class is now able to take part in the wonderful world of LINQ so we can do the usual LINQ things like query the contents, reverse the contents without affecting the original array, get the first item, and so on:

var array = new byte[] { 5, 8, 9, 20, 70, 44, 2, 4 };
array.Dump();
var segment = new ArraySegment<byte>(array, 2, 3);
segment.Dump(); // output: 9, 20, 70
segment.Reverse().Dump(); // output 70, 20, 9
segment.Any(s => s == 99).Dump(); // output false
segment.First().Dump(); // output 9
array.Dump(); // no change

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

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