Udostępnij za pośrednictwem


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

  • Anonymous
    December 21, 2004
    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.
  • Anonymous
    December 21, 2004
    The comment has been removed
  • Anonymous
    December 21, 2004
    What are the prototypes of the other Thread constructors? I only have the 1.1 docs here and Thread only has one constructor.
  • Anonymous
    December 21, 2004
    Im guessing you need to do something like this:

    new Thread(new ThreadStart(delegate { Console.WriteLIne("On another thread"); })).Start();
  • Anonymous
    December 21, 2004
    The comment has been removed
  • Anonymous
    December 21, 2004
    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. :-)
  • Anonymous
    December 21, 2004
    First Close/Dispose and now this! :)
  • Anonymous
    December 21, 2004
    Some guy -- thanks, fixed ;-) I hope my mother doesn't see this ;-)
  • Anonymous
    December 21, 2004

    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");
    });
  • Anonymous
    December 21, 2004
    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!
  • Anonymous
    December 21, 2004
    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. ...
  • Anonymous
    December 22, 2004
    Yes, with the parameterless delegate, the compiler can figure out that you're using the parameterless ThreadStart delegate.
  • Anonymous
    January 03, 2005
    It will not compile <Span>
  • 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