Kaiser69
Kaiser69

Reputation: 430

Thread using delegate or function name

Weird question. What the difference using those piece of code?

class TestThread {

public void waitFunction() {
   // Some code like this.UpdateProgress()
}

public void start() {
    Thread thWaitingScraper = new Thread(waitFunction);   // Method 1

    Thread thWaitingScraper = new Thread(delegate() { waitFunction(); });   // Method 2
}

Thanks!

Upvotes: 1

Views: 72

Answers (1)

MaMazav
MaMazav

Reputation: 1885

No functional difference, but a typeleak can be caused in the second method.

Typeleak is caused when the compiler needs to create an implicit class behind the scene. In this case, because twaitFunction is a non static member of the class, the compiler needs to create a class which holds the reference to this class so the function can be called with the appropriate instance. Within this class it creates the anonymous method you wrote in the second method and passes it as the Thread delegate parameter.

Upvotes: 2

Related Questions