Why is there a need to convert a value (for example short) to string, and then to Int32. Why can it not be converted from short to Int 32?
2 Answers
There is no need to even to any sort of explicit conversion:
short s = 23;
int k = s;
Also, any numeric literals (without any sort of suffix) are int32s anyway.
-- Edit
The reason an explicit cast isn't required is because a short is always smaller than an int, thus a short will always completely fit into the size of an int, so no potential loss of data.
Comments
You don't need this because you can cast:
short shortNumber = 11;
int notAsShortNumber = (int)shortNumber;
13 Comments
Noon Silk
No, there is no need for the explicit cast.
ChaosPandion
It is simply from lack of experience.
Noon Silk
DotNetRookie: It's bad code. It is not required, and is just plain wrong.
Noon Silk
ChaosPandion: No, it doesn't make it clear, it makes it confusing, because there is no need for the cast. It's better to understand how the language works, then things are truly clear.
Noon Silk
ChaosPandion: No, it's bad code regardless! Everyone needs to learn, and there is nothing wrong with being wrong, but it's just foolish to act like it isn't bad to do that. What an odd statement.
|