Csharp/CSharp Tutorial/Data Type/byte box unbox
Boxing short
<source lang="csharp">using System; using System.Collections; class MainClass {
static void Main(string[] args) { short s = 25; Console.WriteLine("short s = {0}", s); Console.WriteLine("short is a: {0}", s.GetType().ToString()); }
}</source>
short s = 25 short is a: System.Int16
Box the value type into a reference type
<source lang="csharp">using System; using System.Collections; class MainClass {
static void Main(string[] args) { short s = 25; object objShort = s; Console.WriteLine("Boxed object is a: {0}", objShort.GetType().ToString()); }
}</source>
Boxed object is a: System.Int16
unbox the reference back into a short
<source lang="csharp">using System; using System.Collections; class MainClass {
static void Main(string[] args) { short s = 25; object objShort = s; short anotherShort = (short)objShort; Console.WriteLine("short anotherShort = {0}", anotherShort); Console.WriteLine("Unboxed object is a: {0}", anotherShort.GetType().ToString()); }
}</source>
short anotherShort = 25 Unboxed object is a: System.Int16