I had following piece of code
try
{
object s = new object();
s = 10;
Console.WriteLine("{0}", Convert.ToInt16(s));
Console.WriteLine("{0}", (Int16)s);
}
catch (InvalidCastException ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
on the basis of which I have lots of questions..
1) Is Convert.ToInt16() and (Int16) both are Unboxing operations
2) If both are related to Unboxing then why they are different. Because the above code shows the following error when Console.WriteLine("{0}", (Int16)s);
line is complied
Error:
Specified cast is not valid
3) As i know (Int16)
is conventional casting and Convert.ToInt16()
is Type safe conversion. But what makes these different here?
Maybe it's a silly question but I'm confused. Please clarify this for me and correct me where I'm wrong.
Best Answer
The numeric literal
10
is treated as an integer, and more specifically anInt32
. Though you typed your variable asobject
, under the covers it is still the integer. You can only unbox a value type to its same type, or to a nullable version of that type, directly.For example, this code:
Will not execute, because the original value of
i
is not a short, it is an integer. You have to first unbox to integer, then you can cast to short.Convert.ToInt16
sidesteps that issue, and the way it does it is an implementation detail. However, that method has multiple overloads that accepts multiple types, including strings, so it is not the equivalent of code using a direct cast.Edit: I noticed I'm mixing terms here, so just so it's clear for a novice C# reader, the names
short
andInt16
are interchangeable for a 16 bit integer, as are the namesint
andInt32
for 32 bit integers. In C#,short
andint
are aliases for the .NET typesInt16
andInt32
, respectively.