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();

  }

}

 

Published 21 December 04 02:45 by BradA
Filed under:

Comments

# Eric Maino said on December 21, 2004 3:44 PM:
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.
# tum said on December 21, 2004 3:46 PM:
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 :)
# tum said on December 21, 2004 3:49 PM:
What are the prototypes of the other Thread constructors? I only have the 1.1 docs here and Thread only has one constructor.
# Matt Berther said on December 21, 2004 4:06 PM:
Im guessing you need to do something like this:

new Thread(new ThreadStart(delegate { Console.WriteLIne("On another thread"); })).Start();
# tum said on December 21, 2004 4:29 PM:
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.
# some guy said on December 21, 2004 4:52 PM:
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. :-)
# James Manning said on December 21, 2004 7:35 PM:
First Close/Dispose and now this! :)
# Brad Abrams[MSFT] said on December 21, 2004 8:00 PM:
Some guy -- thanks, fixed ;-) I hope my mother doesn't see this ;-)
# Judah said on December 21, 2004 8:54 PM:

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");
});
# Joe Duffy said on December 21, 2004 9:31 PM:
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!
# tum said on December 21, 2004 10:17 PM:
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. ...
# Judah said on December 22, 2004 4:12 PM:
Yes, with the parameterless delegate, the compiler can figure out that you're using the parameterless ThreadStart delegate.
# Brad Abrams said on December 27, 2004 2:55 PM:
# Managed from down under said on December 28, 2004 6:31 AM:
# Shon Dot Net Dude said on January 3, 2005 11:21 PM:
It will not compile <Span>
# Community Blogs said on July 16, 2006 11:14 AM:
Just to prove that I am a true geek I thought I would tackle Brad Abrams latest C# quiz. The simple answer
New Comments to this post are disabled

Search

Go

This Blog

Syndication

Page view tracker