3

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?

1
  • This is a very sensible rookie question. Commented Sep 2, 2009 at 4:22

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

You don't need this because you can cast:

short shortNumber = 11;
int notAsShortNumber = (int)shortNumber;

13 Comments

No, there is no need for the explicit cast.
It is simply from lack of experience.
DotNetRookie: It's bad code. It is not required, and is just plain wrong.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.