例如: publicstructStudent{ publicintnum; publicvoidshow() { }

时间:2022-03-03 07:41:26

 1.值类型和引用类型 

 

 值类型和引用类型

常用的数据类型
整形 int
浮点型 foalt
双精度浮点型 double
字符串 string
布尔 bool
枚举 enum
值类型
      值类型担任与System.ValueType类,每个值类型的东西都有一个独立的内存区域用于生存本身的值,值类型数据地址的内存区域称为栈(Stack)。只要在代码中改削它,,就会在它的内存区域内生存这个值。

引用类型
     引用类型担任与System.Object类,在C#中引用类型主要包孕数组、类和接口等。

细分值类型和引用类型
值类型:

根基数据类型:

整形 int
长整形 long
浮点型 foalt
双精度浮点型 double
字符型 char
布尔型 bool

 

2.布局体  

 

1.界说:  访谒修饰符  struct   布局名  {            //布局体  }  

(1)布局体可以有字段,要领,字段不能赋初始值。  (2)可以不new,但有条件,布局体中有成员变量和成员要领,成员变量没有赋值,挪用成员要领,不new会报错,所以一般先new。

  

例如:

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

publicstructStudent{  publicintnum;  publicvoidshow()  {  }}

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

   

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

staticvoidMain(string[] args)  {      Student stu;      stu.num;       stu.show();    }  //没有给成员变量赋值,报错

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

    

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

staticvoidMain(string[] args)  {      Student stu;      stu.num=10;       stu.show();    }  //给成员变量赋值,不报错

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

   

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

staticvoidMain(string[] args)  {      Student stu=newStudent();      stu.num;       stu.show();   }   //new出来,不报错


例如: publicstructStudent{ publicintnum; publicvoidshow() { }

 3.装箱和拆箱  

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

  

 

 

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

 

 

 

值类型 =============> 引用类型   (装箱)

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

    

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

  引用类型 =============> 值类型   (拆箱)

例如: publicstructStudent{ publicintnum; publicvoidshow() { }

   

 

1.要制止装箱和拆箱因为会减低措施性能。

 

2.引用方法参数通报  

(根基数据类型,如,整形,浮点型,字符型,bool行及布局属于值类型;数组,接口和类属于引用类型)  

  使用值方法(不用ref修饰)通报值类型参数时,参数在要领中的改削不会保存  

  使用值方法(不用ref修饰)通报引用类型参数时,参数在要领中的改削会保存  

  使用引用方法(用ref修饰)通报值类型或引用类型参数时,参数在要领中的改削城市保存