有什么用ArraySegment&LT的; T>类? [英] what is the use of ArraySegment<T> class?

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

问题描述

我对面传来了 ArraySegment<字节> 类型,而继承了 MessageEn codeR

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&LT; T&GT; 已成为一个的很多.NET的更多有用的4.5 ,因为它现在实现:

ArraySegment<T> has become a lot more useful in .NET 4.5 as it now implements:

  • 的IList&LT; T&GT;
  • 的ICollection&LT; T&GT;
  • 的IEnumerable&LT; T&GT;
  • 的IEnumerable
  • IReadOnlyList&LT; T&GT;
  • IReadOnlyCollection&LT; T&GT;
  • 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天全站免登陆