Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
« Friday Tutorial Fun | Main | More Open Day Fun »
Thursday
Feb242011

Programming Puzzler

Quick test for all you programming experts. Will this stupid code compile?

public int InfiniteLoop()
{
    while (true)
    {
        Console.WriteLine("Loopy");
        System.Threading.Thread.Sleep(1000);
    }
}

Note: All the namespaces are in place and the WriteLine and the Sleep are perfectly legal calls.

Reader Comments (9)

No, there is no return for the int required
February 24, 2011 | Unregistered CommenterErik SIjnja
Yes, there is no need for a return if the function does not end
February 24, 2011 | Unregistered CommenterSid
It would not compile because the function is supposed to return an integer type, but does not do that in the function, if it was a void function then it would run an infinite loop which stops every 1000th of a second
February 24, 2011 | Unregistered CommenterBenjamin
If there was a return line of code, the complier will flag an error saying line of code unreachable, because it would never get to the return until the program is forcefully terminated.
February 24, 2011 | Unregistered CommenterBenjamin
Well it did for me. :-) I had to make it static to call it from main() though.
February 24, 2011 | Unregistered CommenterAlfred Thompson
@Sid: U R so right.
February 24, 2011 | Unregistered CommenterErik Sijnja
what's the answer ?
February 24, 2011 | Unregistered CommenterAlice
Probably give a warning on the int return :)
February 24, 2011 | Unregistered CommenterJames
would the fact it doesny compile that bit of code not cause a runtime error later on?
October 3, 2011 | Unregistered CommenterJay Wakefield

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.