Any boxing with IndexOf() ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a byte array (myArray) filled by reading bytes from the Com port. And
I need to check for a termination byte (0x00 or "\0") as well as for a
combination of bytes/ASCII characters (myString).

I have to provide best possible performance.

I'm wunderring if there is any internal boxing involved in the statements
like the following:

byte myByte = 0x00;
if (Array.LastIndexOf(myArray, myByte) >= 0)
...

String sbuffer = Encoding.ASCII.GetString(myArray, 0, cnt);
if (sbuffer.LastIndexOf(myString) >= 0)
...

Thanks,
Valentina
 
ValyaS said:
I have a byte array (myArray) filled by reading bytes from the Com port. And
I need to check for a termination byte (0x00 or "\0") as well as for a
combination of bytes/ASCII characters (myString).

I have to provide best possible performance.

I'm wunderring if there is any internal boxing involved in the statements
like the following:

byte myByte = 0x00;
if (Array.LastIndexOf(myArray, myByte) >= 0)
...

Yes, that involves boxing because the parameter type is object.
String sbuffer = Encoding.ASCII.GetString(myArray, 0, cnt);
if (sbuffer.LastIndexOf(myString) >= 0)
...

There's no boxing in that bit.
 
ValyaS said:
And how about the

Encoding.ASCII.GetString(myArray, 0, cnt) ?

No - there's no boxing there. Boxing only occurs when a value type
needs to be treated as an object. myArray is already a reference type,
and the parameters to GetString are (byte[], int, int) so there's no
need to convert either 0 or cnt into an object.
 
Back
Top