Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Stream.ReadByte return int? [duplicate]

Tags:

c#

.net

stream

I spent some time on research System.IO namespace and after that I've found some interesting place for me. Stream class contains method with name ReadByte and definition like that:

public virtual int ReadByte()

So actually my question is why method called ReadByte and returns int? What is the purpose of naming like that?

like image 256
ivamax9 Avatar asked Jul 25 '15 22:07

ivamax9


1 Answers

ReadByte returns

The unsigned byte cast to an Int32, or -1 if at the end of the stream.

Such approach (having a special value which does not collide with any possible byte value) leads to use of a an integral type which "extends" byte.

In principle, types like short would work as well, but it is much more natural and conventional to use int.

like image 198
AlexD Avatar answered Sep 20 '22 20:09

AlexD