2024-04-14 06:00:02

c#中的数组片

你是怎么做到的?给定一个字节数组:

byte[] foo = new byte[4096];

我如何得到数组的前x字节作为一个单独的数组?(具体来说,我需要它作为一个IEnumerable<byte>)

这是用于使用Sockets的。我认为最简单的方法是数组切片,类似于perl语法:

@bar = @foo[0..40];

将前41个元素返回到@bar数组中。c#中是否有我遗漏的东西,或者有其他我应该做的事情?

LINQ对我来说是一个选择。NET 3.5),如果有帮助的话。


当前回答

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()。

其他回答

数组是可枚举的,所以你的foo已经是IEnumerable<字节>本身。 简单地使用像Take()这样的LINQ序列方法来获得你想要的结果(不要忘记在使用System.Linq;时包含LINQ命名空间):

byte[] foo = new byte[4096];

var bar = foo.Take(41);

如果你真的需要一个来自任何IEnumerable<byte>值的数组,你可以使用ToArray()方法。但在这里,情况似乎并非如此。

下面是一个使用泛型的扩展函数,其行为类似于PHP函数array_slice。允许负偏移量和负长度。

public static class Extensions
{
    public static T[] Slice<T>(this T[] arr, int offset, int length)
    {
        int start, end;

        // Determine start index, handling negative offset.
        if (offset < 0)
            start = arr.Length + offset;
        else
            start = offset;

        // Clamp start index to the bounds of the input array.
        if (start < 0)
            start = 0;
        else if (start > arr.Length)
            start = arr.Length;

        // Determine end index, handling negative length.
        if (length < 0)
            end = arr.Length + length;
        else
            end = start + length;

        // Clamp end index to the bounds of the input array.
        if (end < 0)
            end = 0;
        if (end > arr.Length)
            end = arr.Length;

        // Get the array slice.
        int len = end - start;
        T[] result = new T[len];
        for (int i = 0; i < len; i++)
        {
            result[i] = arr[start + i];
        }
        return result;
    }
}
byte[] foo = new byte[4096]; 

byte[] bar = foo.Take(40).ToArray();

你可以使用ArraySegment<T>。它非常轻量级,因为它不复制数组:

string[] a = { "one", "two", "three", "four", "five" };
var segment = new ArraySegment<string>( a, 1, 2 );

下面是一个简单的扩展方法,它返回一个slice作为一个新数组:

public static T[] Slice<T>(this T[] arr, uint indexFrom, uint indexTo) {
    if (indexFrom > indexTo) {
        throw new ArgumentOutOfRangeException("indexFrom is bigger than indexTo!");
    }

    uint length = indexTo - indexFrom;
    T[] result = new T[length];
    Array.Copy(arr, indexFrom, result, 0, length);

    return result;
}

然后你可以这样使用它:

byte[] slice = foo.Slice(0, 40);