C# 不能使用的方法

来源:互联网 发布:mysql好学吗 编辑:程序博客网 时间:2024/06/08 06:54

1.  属性

设置一个属性   

Public    返回值类型   属性名

         Get     返回值                                          取属性值

         Set                                                                     设置属性值

 

 

         编译器生成代码

.method public hidebysig specialname instance int32

        get_变量名() cil managed

{

  // Code size       7 (0x7)

  .maxstack  1

  .locals init ([0] int32 CS$1$0000)

  IL_0000:  nop

  IL_0001:  ldc.i4.1

  IL_0002:  stloc.0

  IL_0003:  br.s       IL_0005

  IL_0005:  ldloc.0

  IL_0006:  ret

} // end of method Class1::get_Item

 

.method public hidebysig specialname instance void

        set_变量名(int32 'value') cil managed

{

  // Code size       2 (0x2)

  .maxstack  8

  IL_0000:  nop

  IL_0001:  ret

} // end of method Class1::set_Item

 

.property instance int32 变量名()

{

  .get instance int32 ConsoleApplication2.Class1::get_变量名()

  .set instance void ConsoleApplication2.Class1::set_变量名(int32)

} // end of property Class1::Item

 

所以可以看出 当我们写了属性后 编译器自己添加方法int  get_变量名()  void set_变量名(int32 i)

自然我们就不能再写方法

Public int get_变量名()

{}

 

Public void Set_变量名(int index)

{}

2.  方法

 public int this[int index]

 {

     get

     {

        return 1;

     }

     }

 

     编译器生成代码

 

.method public hidebysig specialname instance int32

        get_Item(int32 index) cil managed

{

  // Code size       7 (0x7)

  .maxstack  1

  .locals init ([0] int32 CS$1$0000)

  IL_0000:  nop

  IL_0001:  ldc.i4.1

  IL_0002:  stloc.0

  IL_0003:  br.s       IL_0005

  IL_0005:  ldloc.0

  IL_0006:  ret

} // end of method Class1::get_Item

 

 

.property instance int32 Item(int32)

{

  .get instance int32 ConsoleApplication2.Class1::get_Item(int32)

} // end of property Class1::Item

 

编译器自己添加方法int  get_Itemint  index

自然我们也不能添加方法 int  get_Item(int  index)

原创粉丝点击