C# Quiz: Will that compile?


A recent internal thread and a little nudge inspired me to offer this little quiz to keep the old grey matter working over the holiday break. 

In V2.0, does this code compile?  If not why not and how would you fix it? 

 

Obviously the quiz is a little more challenging if you attempt it with out the aid of the compiler…

 

using System;

using System.Threading;

 

public class Class1

{

  public static void Main () {

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

  }

}

 

Comments (16)

  1. Eric Maino says:

    This should not compile becuase the compiler can’t tell of which type the anonymous delegate is.

    If the function only took one type of delegate the code should compile beucase you wouldn’t have any ambiguity.

  2. tum says:

    I can’t really see why it wouldn’t unless the spec changed since I last looked. You aren’t required to provide anonymous method parameter lists anymore are you?

    I assume the line wrapping within a string literal is an html thing not your actual code πŸ™‚

  3. tum says:

    What are the prototypes of the other Thread constructors? I only have the 1.1 docs here and Thread only has one constructor.

  4. Matt Berther says:

    Im guessing you need to do something like this:

    new Thread(new ThreadStart(delegate { Console.WriteLIne("On another thread"); })).Start();

  5. tum says:

    new Thread(new ThreadStart(delegate { Console.WriteLIne("On another thread"); })).Start();

    I don’t think that’s it. threadstart *is* a delegate.

    Assuming Thread.ctor takes two different types of delegates, the solution would be to provide an explicit parameter list for the anonymous delegate

    i.e

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

    Assuming Thread and ThreadStart look like they do in ver 1.1.

  6. some guy says:

    they say a freudian slip is when you say one thing but mean your mother. or in this case, "the old grey mater" which is not a very kind way of referring to her. πŸ™‚

  7. First Close/Dispose and now this! πŸ™‚

  8. Some guy — thanks, fixed πŸ˜‰ I hope my mother doesn’t see this πŸ˜‰

  9. Judah says:

    new Thread(delegate

    {

    Console.WriteLine("On another thread");

    }).Start();

    Would work if 1.1 supported anonymous methods. πŸ™‚ But in 2.0, Thread.Start has overloads allowing a parameterized thread start, which means the compiler can’t figure out which one to use, ThreadStart or ParameterizedThreadStart. So no, it won’t compile.

    This should compile:

    // tell the compiler it should be a ThreadStart

    new Thread((ThreadStart)delegate

    {

    Console.WriteLine("On another thread");

    });

  10. Joe Duffy says:

    Judah brings up an interesting point…

    Who would’ve thunk that introducing a new ctor overload would end up breaking people? Interestingly, though, if we had anonymous delegate support in v1.1 and folks coded up something like what you’ve shown, introducing the new Thread.ctor(ParameterizedThreadStart) overload would prevent that same source from compiling without modification on v2.0.

    Luckily this isn’t the case. πŸ™‚

    Hmm!

  11. tum says:

    Judah,

    I suspected they may have added a thread start with state attached (tho that wouldn’t be needed if you used an anonymous method worth closures ;)).

    Does this compile?

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

    The empty parameter list should be enough for the compiler to work out which delegate type you want constructed. …

  12. Judah says:

    Yes, with the parameterless delegate, the compiler can figure out that you’re using the parameterless ThreadStart delegate.

  13. Shon Dot Net Dude says:

    It will not compile <Span>

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