Udostępnij za pośrednictwem


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

  • Anonymous
    December 27, 2004
    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.

  • Anonymous
    December 27, 2004
    The comment has been removed

  • Anonymous
    December 27, 2004
    I prefer #1; it's the smallest amount of code that works.

  • Anonymous
    December 27, 2004
    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.

  • Anonymous
    December 27, 2004
    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?

  • Anonymous
    December 27, 2004
    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.

  • Anonymous
    January 05, 2005
    I like #5. It's more re-usable, cleaner to read, and just 'feels' best.

  • Anonymous
    January 18, 2005
    The comment has been removed

  • Anonymous
    July 16, 2006
    Just to prove that I am a true geek I thought I would tackle Brad Abrams latest C# quiz. The simple answer

  • Anonymous
    May 29, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=brad-abrams-c-quiz-will-that-compile-answer

  • Anonymous
    June 07, 2009
    PingBack from http://greenteafatburner.info/story.php?id=814

  • Anonymous
    June 08, 2009
    PingBack from http://quickdietsite.info/story.php?id=3389

  • Anonymous
    June 09, 2009
    PingBack from http://weakbladder.info/story.php?id=7053

  • Anonymous
    June 09, 2009
    PingBack from http://cellulitecreamsite.info/story.php?id=5122

  • Anonymous
    June 15, 2009
    PingBack from http://debtsolutionsnow.info/story.php?id=3315

  • Anonymous
    June 15, 2009
    PingBack from http://einternetmarketingtools.info/story.php?id=6034

  • Anonymous
    June 16, 2009
    PingBack from http://topalternativedating.info/story.php?id=4236

  • Anonymous
    June 19, 2009
    PingBack from http://mydebtconsolidator.info/story.php?id=13688