你是怎么做到的?给定一个字节数组:
byte[] foo = new byte[4096];
我如何得到数组的前x字节作为一个单独的数组?(具体来说,我需要它作为一个IEnumerable<byte>)
这是用于使用Sockets的。我认为最简单的方法是数组切片,类似于perl语法:
@bar = @foo[0..40];
将前41个元素返回到@bar数组中。c#中是否有我遗漏的东西,或者有其他我应该做的事情?
LINQ对我来说是一个选择。NET 3.5),如果有帮助的话。
你是怎么做到的?给定一个字节数组:
byte[] foo = new byte[4096];
我如何得到数组的前x字节作为一个单独的数组?(具体来说,我需要它作为一个IEnumerable<byte>)
这是用于使用Sockets的。我认为最简单的方法是数组切片,类似于perl语法:
@bar = @foo[0..40];
将前41个元素返回到@bar数组中。c#中是否有我遗漏的东西,或者有其他我应该做的事情?
LINQ对我来说是一个选择。NET 3.5),如果有帮助的话。
当前回答
如果你想要IEnumerable<byte>,那么只是
IEnumerable<byte> data = foo.Take(x);
其他回答
static byte[] SliceMe(byte[] source, int length)
{
byte[] destfoo = new byte[length];
Array.Copy(source, 0, destfoo, 0, length);
return destfoo;
}
//
var myslice = SliceMe(sourcearray,41);
您可以使用Take扩展方法
var array = new byte[] {1, 2, 3, 4};
var firstTwoItems = array.Take(2);
可以使用数组的CopyTo()方法。
或者使用LINQ,你可以使用Skip()和Take()…
byte[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
var subset = arr.Skip(2).Take(2);
c# 8现在(从2019年开始)支持范围,这让你更容易实现Slice(类似于JS语法):
var array = new int[] { 1, 2, 3, 4, 5 };
var slice1 = array[2..^3]; // array[new Range(2, new Index(3, fromEnd: true))]
var slice2 = array[..^3]; // array[Range.EndAt(new Index(3, fromEnd: true))]
var slice3 = array[2..]; // array[Range.StartAt(2)]
var slice4 = array[..]; // array[Range.All]
你可以使用范围来代替众所周知的LINQ函数:Skip(), Take(), Count()。
您可以对原始数组(即IList)使用包装器,就像下面(未经测试的)代码一样。
public class SubList<T> : IList<T>
{
#region Fields
private readonly int startIndex;
private readonly int endIndex;
private readonly int count;
private readonly IList<T> source;
#endregion
public SubList(IList<T> source, int startIndex, int count)
{
this.source = source;
this.startIndex = startIndex;
this.count = count;
this.endIndex = this.startIndex + this.count - 1;
}
#region IList<T> Members
public int IndexOf(T item)
{
if (item != null)
{
for (int i = this.startIndex; i <= this.endIndex; i++)
{
if (item.Equals(this.source[i]))
return i;
}
}
else
{
for (int i = this.startIndex; i <= this.endIndex; i++)
{
if (this.source[i] == null)
return i;
}
}
return -1;
}
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
public T this[int index]
{
get
{
if (index >= 0 && index < this.count)
return this.source[index + this.startIndex];
else
throw new IndexOutOfRangeException("index");
}
set
{
if (index >= 0 && index < this.count)
this.source[index + this.startIndex] = value;
else
throw new IndexOutOfRangeException("index");
}
}
#endregion
#region ICollection<T> Members
public void Add(T item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(T item)
{
return this.IndexOf(item) >= 0;
}
public void CopyTo(T[] array, int arrayIndex)
{
for (int i=0; i<this.count; i++)
{
array[arrayIndex + i] = this.source[i + this.startIndex];
}
}
public int Count
{
get { return this.count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
for (int i = this.startIndex; i < this.endIndex; i++)
{
yield return this.source[i];
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}