C#基础学习(一)

 

     装箱与拆箱

     装箱:装箱转换是指一个值类型隐式地转换成一个Object类型,或者把这个值类型转换成个被该值类型应用的接中类型(interface-type).把个值类型的值装箱,也就是创建了一个object实例并将这个值自制给这个object.

     using System

          class Test{

                    public static void Main(){

                    int i= 10;

                    object obj = i;//对象类型

                    if(obj is int){

                         Console.Write("The value of i is boxing")

                              }

                    i= 20;//改变I的值

                    Console.WriteLine("int:i = {0}",i);

 

                    Console.WriteLine("object:obj = {0}",obj);             

                     }

          

          }

 

          输出结果:

          The value of i is boxing

          int:i = 20;

          object:obj= 10;

          说明,被装箱的类型的值是作为一个拷贝赋给对象的.

          拆箱转换.

          指一个对象类型显式地转换成一个值类型,或是将一个接口类型显式地转换成一个执行该接口的值类型.

          int i=10;

          object obj = i;

          int j = (int)obj;

posted @ 2008-07-29 23:00  liuyan  阅读(144)  评论(0编辑  收藏  举报