Stream.ReadByte

Why does the ReadByte method of a stream return an int instead of a byte? According to reflector it is returning the first element of a byte array, implicitly casted to an int. I’m then casting this straight back to a byte… I’m not sure what the benefit is of this!

Advertisements

One Response to Stream.ReadByte

  1. CPUs are optimized to deal with 32 bits integers.

    .Net team seemed to know this and, therefore, they used “int” most of the times. Apparently, it’s CPU-cheapear to cast back and forth to byte (which actually, they only mask the first 8 bits of the integer) and use the CPU-optimization for summing, multiplying, etc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: