将List和Array作为IEnumerable的参数传递

Sae*_*ani 0 .net c# generics resharper ienumerable

我想创建一个方法,可以接受两个List<byte>和字节数组作为参数(如Resharper建议):

public static UInt16 GetSourceAddress(IEnumerable<byte> packet)
{
    return BitConverter.ToUInt16(new[] {packet[4], packet[5]}, 0);
}
Run Code Online (Sandbox Code Playgroud)

Bute我得到了以下编译错误:

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<byte>'
Run Code Online (Sandbox Code Playgroud)

我知道我可以继续使用List和byte []进行两次重载,但这个问题表明了什么?怎么解决?

Jon*_*eet 6

如果您想要随机访问,请IList<T>改用:

public static UInt16 GetSourceAddress(IList<byte> packet)
Run Code Online (Sandbox Code Playgroud)

无论List<byte>byte[]实施IList<byte>,它有一个索引.