C# Quiz: Will that compile? [Answer]

Some good comments on my recent quiz. Many of you are hitting on the right thing…


The answer is no, this will not compile as is.   This is because in V2.0 we added a new constructor to Thread that allows you to pass a ParameterizedThreadStart delegate. 


Jay responded to the thread with several good fixes…


  1. Be explicit about the parameter list:


            new Thread(delegate () { Console.WriteLine(“On another thread”); }).Start();


  1. Select the overload & delegate type via casting:


            new Thread((ParameterizedThreadStart) delegate { Console.WriteLine(“On another thread”); }).Start();


  1. Explicitly create a delegate instance:


new Thread(new ParameterizedThreadStart(delegate { Console.WriteLine(“On another thread”); })).Start();


  1. Use a local temp:


            ParameterizedThreadStart start = delegate { Console.WriteLine(“On another thread”); };

            new Thread(start).Start();


  1. Don’t use an anonymous method:


            new Thread(MyThreadStart).Start();


        static void MyThreadStart(object obj)


            Console.WriteLine(“On another thread”);




What do you think the “best” solution is?  Clearly VS could be a little more helpful in writing this kind of code… what sort of VS support would you suggest?



Comments (18)

  1. Mike Dunn says:

    Being a C++ guy (not a C#) guy, I would of course go for #5. 🙂 But my second choice is #4, since those nested news and inline code can get unreadable very quickly.

    IMO VS should by default produce readable code, avoiding anything like #3 altogether, which will only encourage devs to write similarly-unreadable code.

  2. I also like #4 but it’s also a temporary field and can be avoided. So then I thought #3 wasn’t that bad, but as Mike says it can be unreadable. But it also depends on how big this line of code can be.

    #5 is also nice, it’s also very readable, anonymous methods are mostly unreadable code; you must think a little extra when you read codes with anonymous methods. Maybe this is because we aren’t used to the syntax yet?

    About the VS issue… humm… I think it shall be up to the developer to decide how they write there own code. No tools shall do it. Because I can’t see this case unique, there are more places where this kind of issue can appear.



  3. Robert Taylor says:

    I prefer #1; it’s the smallest amount of code that works.

  4. Anon says:

    I think there doesn’t need to be tool support for something like this – at some point it’s OK to ask programmers to think and/or write code.

    If the tool feels compelled to do something, it should do #4 or #5 (maybe depending on how long the delegate definition was going to be). They are the most readable options, and therefore the best defaults; options #1-3 are abominations.

  5. Judah says:

    You know, since we’ve been using .NET 2 beta extensively at work, I can say that anonymous methods are one of the most useful new features, rivaling even generics.

    That said, I feel uneasy writing many anonymous methods as the compiler generates nested classes, instanciates them, then calls a class method in place of the anonymous methods; seems like a lot of overhead, yet is such a useful feature of the language that I’d be hard pressed to go without it… Brad, could you give us some useful information on how anonymous methods are generated by the compiler and what the trade off is — i.e. in performance intensive scenarios, should we be avoiding heavy use of anonymous methods or is the overhead small enough that the GC wouldn’t be bothered much?

  6. Arild Fines says:

    Number 1, definitely – short and succinct.

    And I really don’t understand this bias against closures/anonymouse methods. They are, for short event handlers/predicates et, far more readable than if you create a separate method. It’s a rather reactionary attitude, I think.

  7. I like #5. It’s more re-usable, cleaner to read, and just ‘feels’ best.

  8. online poker says:

    You may find it interesting to check some helpful info about online poker http://online-poker.online-deals-4u.info/

    phentermine http://phentermine.online-deals-4u.info/

    cialis http://cialis.online-deals-4u.info/

    tramadol http://tramadol.online-deals-4u.info/

    poker http://poker.online-deals-4u.info/

    online casino http://online-casino.online-deals-4u.info/

    casinos http://casinos.online-deals-4u.info/

    online casinos http://online-casinos.online-deals-4u.info/

    texas holdem http://texas-holdem.online-deals-4u.info/

    texas holdem poker http://texas-holdem-poker.online-deals-4u.info/

    pacific poker http://pacific-poker.online-deals-4u.info/

    student loans http://student-loans.online-deals-4u.info/

    private mortgages http://private-mortgages.online-deals-4u.info/

    low interest credit cards http://low-interest-credit-cards.online-deals-4u.info/

    credit cards http://credit-cards.online-deals-4u.info/

    diet pills http://diet-pills.online-deals-4u.info/

    weight loss diet pills http://weight-loss-diet-pills.online-deals-4u.info/

    viagra http://viagra.online-deals-4u.info/

    texas hold em http://texas-hold-em.online-deals-4u.info/

    texas hold em poker http://texas-hold-em-poker.online-deals-4u.info/

    poker games http://poker-games.online-deals-4u.info/

    party poker http://party-poker.online-deals-4u.info/

    empire poker http://empire-poker.online-deals-4u.info/

    hold em poker http://hold-em-poker.online-deals-4u.info/

    credit card http://credit-card.online-deals-4u.info/

    payday loans http://payday-loans.online-deals-4u.info/

    loans http://loans.online-deals-4u.info/

    personal loans http://personal-loans.online-deals-4u.info/

    levitra http://levitra.online-deals-4u.info/

    buy phentermine http://buy-phentermine.online-deals-4u.info/

    generic viagra http://generic-viagra.online-deals-4u.info/

    carisoprodol http://carisoprodol.online-deals-4u.info/

    soma http://soma.online-deals-4u.info/

    butalbital http://butalbital.online-deals-4u.info/

    buy viagra http://buy-viagra.online-deals-4u.info/

  9. Just to prove that I am a true geek I thought I would tackle Brad Abrams latest C# quiz. The simple answer