casting Object to byte array

  • Thread starter Thread starter glenn
  • Start date Start date
G

glenn

I have a COM server that is returning a type Object that I need to cast as a
byte array. Can anyone tell me how this is performed in C#?



Thanks,

glenn
 
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.

glenn
 
glenn said:
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.

That's fine, except you're creating a new byte array for no reason -
just generating garbage.

Just use:

byte[] b = (byte[]) myobj;
 
thanks

glenn

Jon Skeet said:
glenn said:
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.

That's fine, except you're creating a new byte array for no reason -
just generating garbage.

Just use:

byte[] b = (byte[]) myobj;
 
Back
Top