Is there any overhead in the use of anonymous methods?

后端 未结 8 1769
遇见更好的自我
遇见更好的自我 2021-02-15 17:27

I would like to know if there is any overhead incurred through the use of anonymous methods when creating a Background worker.

for example:

public void S         


        
8条回答
  •  名媛妹妹
    2021-02-15 17:39

    This is what decompiler said:

    [CompilerGenerated]
    private static DoWorkEventHandler CS$<>9__CachedAnonymousMethodDelegate1;
    
    [CompilerGenerated]
    private static void b__0(object sender, DoWorkEventArgs e)
    {
        throw new NotImplementedException();
    }
    
    public void SomeMethod1()
    {
        BackgroundWorker worker = new BackgroundWorker();
        BackgroundWorker backgroundWorker = worker;
        backgroundWorker.DoWork += (object sender, DoWorkEventArgs e) => throw new NotImplementedException();
        worker.RunWorkerAsync();
    }
    
    public void SomeMethod2()
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += worker_DoWork;
        worker.RunWorkerAsync();
    }
    
    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        throw new NotImplementedException();
    } 
    

    Edit:

    Looking at IL code there is only small overhead in creating/assigning method to delegate for the first time.

提交回复
热议问题