-2

ref. to your reply: "What happens when you cast from short to byte in C#?"

You said since short is 2 byte and byte is 1 byte hence even if short value is within range of byte it does not get implicitly converted to byte.

Then how come with in range int value is implicitly converted and stored in short ?

Community
  • 1
  • 1
Rahul Utb
  • 489
  • 2
  • 7
  • 15
  • `Then how come with in range int value is implicitly converted and stored in int?`. You mean `converted and stored in short`, right? – Marcel N. Aug 15 '14 at 21:12
  • yes you are right sir...! I have edit my query for that typo.. – Rahul Utb Aug 15 '14 at 21:15
  • 1
    I think you need to provide an example. There's no implicit conversion from [`int` to `short`](http://msdn.microsoft.com/en-us/library/ybs77ex4.aspx). – Marcel N. Aug 15 '14 at 21:17
  • Most basic sample (`short v = someIntVariable;`) fails to compile with - "Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)"... Definitely need one that shows the problem. – Alexei Levenkov Aug 15 '14 at 21:19
  • Side note: please make sure each question is self-contained, links to other questions are OK, but question should be clear without reading any of linked articles/questions. – Alexei Levenkov Aug 15 '14 at 21:21
  • Sure Alexei I'll keep that in mind next time. Second... you say short v = someIntVariable; fails to compile... but we store values like short v = 10; – Rahul Utb Aug 15 '14 at 21:25
  • And sir Marcel you are right I did some mistake on that... implicit conversion from int to short giving error. But if we can store an int value in short [short v = 10;] Why am I getting error when trying to send int parameter to method with short argument ? Example Add (num1, num2); where num1 and num2 are int... and function is int Add (short x, short y); where my num 1 and num 2 are 10 and 11 respectively... – Rahul Utb Aug 15 '14 at 21:29
  • 1
    Sheck out "short" on MSDN - http://msdn.microsoft.com/en-us/library/ybs77ex4.aspx. Please also *edit* your post so it is clear what exactly you trying to ask (you have many variants of the question in comments). – Alexei Levenkov Aug 15 '14 at 21:51

1 Answers1

1

Alexei Levenkov referenced the answer in his last comment, but maybe there should be some further explanation: assigning a literal to a variable is something different than assigning a value from another variable (especially if the other variable is also of another type, which implies an implicit or explicit conversion).

For example:

// The following statement defines a variable
// called 'integerValue' and assigns the
// 32 bit signed integer value fourty-two to 
// it because of the literal '42'.
int integerValue = 42;

// The next statement defines a variable
// called 'longValue' and assigns the
// 64 bit signed integer value forty-two to
// it because of the literal '42L'
long longValue = 42L;

// The next line defines a variable
// with the 16 bit signed integer value
// forty-two (literal '42') - the compiler
// will automatically interpret the literal
// in the correct format
short shortValue1 = 42;

// There is no implicit conversion between
// int and short, therefore the next statement
// will not compile. Notice that there is no
// literal involved here - instead a value from
// another variable is assigned. 
short shortValue2 = integerValue; // Error

// The following assignment is also not correct as
// '42L' is not a suitable literal for values of
// type short
short shortValue3 = 42L; // Error

The main thing is that int and short use the same style for literals (plain number, no suffix) and that is what might get people confused.

feO2x
  • 5,358
  • 2
  • 37
  • 46
  • 2
    +1. You can also add `short shortValue3 = 42L;` and `short shortValue4 = 0x1FFFF;` which will fail too. – Alexei Levenkov Aug 15 '14 at 22:26
  • hi, thanks feO2x ! I am clear on my doubt when u said "assigning a literal to a variable is something different than assigning a value from another variable..." – Rahul Utb Aug 16 '14 at 05:54
  • sorry, but, I find another query in your reply... why assigning a literal to a variable is something different than assigning a value from another variable... and what is that difference... ! just a lil query if you have sometime to... – Rahul Utb Aug 16 '14 at 05:57
  • If you assign a literal to a variable, this results in the compiler interpreting that particular value as a certain type and hard coding it into the assembly. The compiler can perform compile-time checks doing this. On the other hand, assigning a variable is always performed at run time as the compiler cannot know which specific value is held by a certain variable when the assignment is performed. – feO2x Aug 16 '14 at 06:09
  • In this example, if you'd used a `int` variable and had assigned its value to a `short` variable, then you would have seen a explicit conversion error (like in the example above). But as you used a literal (at least this is what I assume) you stumbled over the fact that `int` and `short` use the same style for literals (as mentioned in my answer). – feO2x Aug 16 '14 at 06:14