score:2

Accepted answer

Pure LINQ:

var arr = new byte[] { 1, 2, 3, 4, 5, 6 };

var res = arr.Zip(arr.Skip(1), (a, b) => new { a, b }).Select((x, i) => new { x, i })
    .FirstOrDefault(v => v.x.a == 3 && v.x.b == 4);


if (res != null)
{
    Console.WriteLine(res.i);
}

score:1

Given how you said you want to search for bytes in a list, I'm assuming you have an object of List<byte>, named list, and a byte[], named bytes.

List<byte> list = new List<byte>();
byte[] bytes = { 0x01, 0x02 };

list.Where((b, i) => (list.Count() >= i + 1 ? false : (b == bytes[0] && list[i + 1] == bytes[1]))).First();

The ternary expression ensures you don't have an ArrayOutOfBoundsException


Related Articles