泛型



使用泛型 C#

using System;
using System.Collections.Generic;
using System.Text;

namespace Ttype
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<int> s = new Stack<int>();
            s.setO_item(2);
            Console.WriteLine(s.getO_item());
        }
    }

    class Stack<T>
    {
        private T i_item;
        public void setO_item(T o)
        {
            i_item = o;
        }
        public T getO_item()
        {
            return i_item;
        }
    }
}

IL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       28 (0x1c)
  .maxstack  2
  .locals init ([0] class Ttype.Stack`1<int32> s)
  IL_0000:  nop
  IL_0001:  newobj     instance void class Ttype.Stack`1<int32>::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.2
  IL_0009:  callvirt   instance void class Ttype.Stack`1<int32>::setO_item(!0)
  IL_000e:  nop
  IL_000f:  ldloc.0
  IL_0010:  callvirt   instance !0 class Ttype.Stack`1<int32>::getO_item()
  IL_0015:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_001a:  nop
  IL_001b:  ret
} // end of method Program::Main


 

.method public hidebysig instance !T  getO_item() cil managed
{
  // Code size       12 (0xc)
  .maxstack  1
  .locals init ([0] !T CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldfld      !0 class Ttype.Stack`1<!T>::i_item
  IL_0007:  stloc.0
  IL_0008:  br.s       IL_000a
  IL_000a:  ldloc.0
  IL_000b:  ret
} // end of method Stack`1::getO_item

.method public hidebysig instance void  setO_item(!T o) cil managed
{
  // Code size       9 (0x9)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldarg.1
  IL_0003:  stfld      !0 class Ttype.Stack`1<!T>::i_item
  IL_0008:  ret
} // end of method Stack`1::setO_item


不使用泛型 C#

using System;
using System.Collections.Generic;
using System.Text;

namespace Ttype
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack s = new Stack();
            s.setO_item(2);
            Console.WriteLine(s.getO_item());
        }
    }

    class Stack
    {
        private int i_item;
        public void setO_item(int o)
        {
            i_item = o;
        }
        public int getO_item()
        {
            return i_item;
        }
    }
}


IL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       28 (0x1c)
  .maxstack  2
  .locals init ([0] class Ttype.Stack s)
  IL_0000:  nop
  IL_0001:  newobj     instance void Ttype.Stack::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.2
  IL_0009:  callvirt   instance void Ttype.Stack::setO_item(int32)
  IL_000e:  nop
  IL_000f:  ldloc.0
  IL_0010:  callvirt   instance int32 Ttype.Stack::getO_item()
  IL_0015:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_001a:  nop
  IL_001b:  ret
} // end of method Program::Main

.method public hidebysig instance int32  getO_item() cil managed
{
  // Code size       12 (0xc)
  .maxstack  1
  .locals init ([0] int32 CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldfld      int32 Ttype.Stack::i_item
  IL_0007:  stloc.0
  IL_0008:  br.s       IL_000a
  IL_000a:  ldloc.0
  IL_000b:  ret
} // end of method Stack::getO_item

.method public hidebysig instance void  setO_item(int32 o) cil managed
{
  // Code size       9 (0x9)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldarg.1
  IL_0003:  stfld      int32 Ttype.Stack::i_item
  IL_0008:  ret
} // end of method Stack::setO_item

 

使用多态 C#

using System;
using System.Collections.Generic;
using System.Text;

namespace Ttype
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack s = new Stack();
            s.setO_item(2);
            Console.WriteLine((int)s.getO_item());
        }
    }

    class Stack
    {
        private object i_item;
        public void setO_item(object o)
        {
            i_item = o;
        }
        public object getO_item()
        {
            return i_item;
        }
    }
}

IL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       38 (0x26)
  .maxstack  2
  .locals init ([0] class Ttype.Stack s)
  IL_0000:  nop
  IL_0001:  newobj     instance void Ttype.Stack::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  ldc.i4.2
  IL_0009:  box        [mscorlib]System.Int32
  IL_000e:  callvirt   instance void Ttype.Stack::setO_item(object)
  IL_0013:  nop
  IL_0014:  ldloc.0
  IL_0015:  callvirt   instance object Ttype.Stack::getO_item()
  IL_001a:  unbox.any  [mscorlib]System.Int32
  IL_001f:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0024:  nop
  IL_0025:  ret
} // end of method Program::Main

.method public hidebysig instance object 
        getO_item() cil managed
{
  // Code size       12 (0xc)
  .maxstack  1
  .locals init ([0] object CS$1$0000)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldfld      object Ttype.Stack::i_item
  IL_0007:  stloc.0
  IL_0008:  br.s       IL_000a
  IL_000a:  ldloc.0
  IL_000b:  ret
} // end of method Stack::getO_item

.method public hidebysig instance void  setO_item(object o) cil managed
{
  // Code size       9 (0x9)
  .maxstack  8
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  ldarg.1
  IL_0003:  stfld      object Ttype.Stack::i_item
  IL_0008:  ret
} // end of method Stack::setO_item

 

你可能感兴趣的:(泛型)