开发者

What is the best way to move the content of an enumerable objects to an array of objects? See code below

开发者 https://www.devze.com 2023-01-30 16:50 出处:网络
public object[] GetByteCodes(IEnumerable<byte> enumerableByteCodes) { object[] objects = new 开发者_JAVA百科object[enumerableByteCodes.Count()];
public object[] GetByteCodes(IEnumerable<byte> enumerableByteCodes)
{

     object[] objects = new 开发者_JAVA百科object[enumerableByteCodes.Count()];

     //What the next step here?...

     return objects;
}


Do those bytes in enumerableByteCodes represent objects? If not, why are you not returning a byte[]?

If you want to return a byteArray, you can just use the LINQ extension method on

IEnumerable<t>.ToArray();

If you want to return it as an object you can use the Select LINQ extension to do that too.

enumerableByteCodes.Select(t=> (object)t).ToArray();

You could also use

enumerableBytes.OfType<object>().ToArray();


byteCodes = enumerableByteCodes.ToArray();

I would simply make the function:

public byte[] GetByteCode(IEnumerable<byte> enumerableByteCodes)
{
    return enumerableByteCodes.ToArray();
}

Why are you using object[], seeing as your generic type is a byte?

Update:

Since you must have an object[], you need to cast each member:

public object[] GetByteCode(IEnumerable<byte> enumerableByteCodes)
{
    return enumerableByteCodes.Select(x => (object)x).ToArray();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号