This question already has answers here:
MemoryStream: why convert to byte after readByte
(3 answers)
Closed 7 years ago.
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?
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.
Related
This question already has answers here:
Can you use Enum for Double variables?
(6 answers)
Closed 3 years ago.
I am trying to create a program in where I am trying to give/assign a created enumeration list, a monetary value to be returned. However I am unsure how I can do this.
public decimal GetAccessoriesCost()
{
?????????
}
public enum Accessories
{
None,
StereoSystem,
LeatherInterior,
StereoAndLeather,
ComputerNavigation,
StereoAndNavigation,
LeatherAndNavigation,
All
}```
I want Stereo to be 20.20
and Leather to be 10.10
The enums in C# (unlike Java) have to be cardinal (something that is basically an integer or a whole number). see this answer for more details Can you use Enum for Double variables?
The reason enums have this constraint is to be closer to c++ and c where the enums are a fancy way to group integer constants in a namespace and probably generate a sequence.
This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
How can I convert a hex string to a byte array? [duplicate]
(4 answers)
Closed 9 years ago.
This is a duplicate question, my apologies everyone!
Firstly, I apologize if this is a simple question, I have been searching for a very long time, and either an answer about this does not exist, the answer I am looking for has been buried under answers to questions about how to convert a string to a byte array, or I'm not searching with the right terminology. I have also found a few answers on converting a single hex value into a byte but applying those methods to work with what I want to do doesn't really seem to work very well.
What I am looking for is not how to convert "string" to a byte array, rather, I am trying to convert an already in bytes value from a textbox, into something my application will recognize as a byte array. I'll try to explain better with an example:
textBox.Text = 019F314A
I want byte[] bytes to equal { 0x01, 0x9F, 0x31, 0x4A }
Hopefully that makes sense. Thanks to anyone who can offer any assistance!
I believe you can use Convert.ToByte(), you might have to slice your string in pairs and loop through it.
If you do a quick search there are many topics on this already on stackoverflow
How do you convert Byte Array to Hexadecimal String, and vice versa?
You can also look at this MS example, it is to convert to int, but the idea is the same.
http://msdn.microsoft.com/en-us/library/bb311038.aspx
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between Convert.ToInt32(string) and Int32.Parse?
are these three exactly the same in performance? or whats the deal?
Parse v. TryParse
Difference between Convert.ToInt32(string) and Int32.Parse?
Convert.ToInt32 works against object and accepts a range of data-types, of which `string is just one.
int.Parse is optimised for the very common string case, and raises an exception on failure. Useful when we expect an integer.
int.TryParse is like int.Parse, but no exception (false instead); useful for testing for an integer.
System.Convert check if the source object implements IConvertible and tehn call the proper method, so any object that "know" how to be converted to int can be converted that way.
Parse and TryParse starts from the string reperesentation. Try parse returns a boolean signaling that conversion succeeded/failed. Parse trows if conversion fails.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C#, why is String a reference type that behaves like a value type?
Why in C# string is a class/ref type , where as int/double are value/struct- any specific reason or it is by design
Integral types have the important property of being accessible as a whole by the processor in one go. It is not the case for a string which may be composed of thousands of bytes, so in all languages, strings have always been pointed to, because the computer cannot really do it any other way.
In an object language like C#, it is canonical to create a class to point to a memory location: that's actually what an object is about.
So yes, strings are classes, because they can't be integral types.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
byte + byte = int… why?
I have a grid from (-1024,-1024) to (1024,1024), so I don't need all the values that an int provides, but I've noticed that all of my algorithms return as ints and I need to typecast them all with (short). Could anyone explain why all math operations return as int and is it more effective to parse as short since math operations return as int?
short yCoordinate = (short)(short.Parse(RtData[1][1]) - 1);
The return an int because most maths operations need larger numbers than a short can contain. The BCL is there for use by many programmers and the creators had to balance readability, usability and performance amongst others in order to create classed that are generally useful.
As method signatures do not take return type into account, they had to decided what would be most useful as a return type (short, int, long etc) for these functions. They decided that int was best.