I want to make a method that can accept both List<byte> and array of bytes as argument (as Resharper suggested):
public static UInt16 GetSourceAddress(IEnumerable<byte> packet)
{
return BitConverter.ToUInt16(new[] {packet[4], packet[5]}, 0);
}
Bute I get the follwing compilation error:
Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<byte>'
I know I can just go ahead with two overloads with List and byte[], but what does this problem indicates? How to resolve it?
If you want random access, use
IList<T>instead:Both
List<byte>andbyte[]implementIList<byte>, and it has an indexer.