C#实现向多线程传参的三种方式实例分析

2019-12-26 14:10:32丽君
  • IL_000b: stloc.0  IL_000c: ldloc.0 
  • IL_000d: ldftn instance void ThreadWithParameters.MyThread::ThreadMain()  IL_0013: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object, native int) 
  • IL_0018: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)  IL_001d: stloc.1 
  • IL_001e: ldloc.1  IL_001f: callvirt instance void [mscorlib]System.Threading.Thread::Start() 
  • IL_0024: nop  IL_0025: call int32 [mscorlib]System.Console::Read() 
  • IL_002a: pop  IL_002b: ret 
  • } // end of method Program::Main 

    比较两端代码,可以发现两者都有一个newobj,这句的作用是初始化一个类的实例,第三种方式由编译器生成了一个类:c__DisplayClass1

     

     
    1. IL_0000: newobj instance void ThreadWithParameters.Program/'<>c__DisplayClass1'::.ctor()  IL_0006: newobj instance void ThreadWithParameters.MyThread::.ctor(string)