locked
Compiler bug when using anonymous types in an if statement?

    Question

  • Just something i noticed and i was wondering, is there something i do wrong or is this a little bug in the compiler:

     

    if (initializeApplication())

    {

    if (StartUpdateFlow(new EventHandler(delegate(object objVoid, EventArgs voidArgs)

    {

    //Custom code

    }))) ;

    }

     

     

    When removing the ; at the end of the second if statement, i get the error:

    error CS1525: Invalid expression term '}

     

    This is probably due to the fact that an anonymous type declaration requires a ; at the end.

     

    When placing the ;, i get a warning

    warning CS0642: Possible mistaken empty statement

     

    As you can see, its not a big problem or anything, i just find it strange behaviour which i would like to have explained

     

    Thanks in advance,

     

    Koen

    Monday, May 26, 2008 11:07 AM

Answers

  • It doesn't look like a bug to me; why would you have an if statement without a body?  You're missing a set of brackets for the if statement (you should have one for the outer if, one for the inner if, and another for the delegate.  For example:

     

    Code Snippet

    if (initializeApplication())

    {

        if (StartUpdateFlow(new EventHandler(delegate(object objVoid, EventArgs voidArgs)

                                                {

                                                    // anonymous method body

                                                })))

        {

            // code when initializeApplication returns true

        }

    }

     

     

     

    What you orignally had was the equivalent of this:

     

    Code Snippet

    EventHandler ev = new EventHandler(delegate(object objVoid, EventArgs voidArgs)

                                           {

                                               // anonymous method body

                                           });

    if (initializeApplication())

    {

        if (StartUpdateFlow(ev))

            ;

    }

     

     

     

    ...and of course, if you removed the semicolon after the call to StartUpdateFlow() you have a syntax error--an if statement with no body.

    Monday, May 26, 2008 2:15 PM

All replies

  • This certainly looks like a bug. I tested the following code in both 2005 and 2008 and both produces the anomaly.

    using System;

    Code Snippet

    namespace ConsoleApplication16
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (Test(new EventHandler(delegate(object objVoid, EventArgs voidArgs)
                {
                    // Custom code
                }))) ;
            }

            private static Boolean Test(EventHandler eh)
            {
                return true;
            }
        }
    }



    I have not installed SP1 beta for 2008 yet, so I don't know if that fixes it, perhaps someone with it installed can just copy the above code and test it.
    Monday, May 26, 2008 11:13 AM
  • It doesn't look like a bug to me; why would you have an if statement without a body?  You're missing a set of brackets for the if statement (you should have one for the outer if, one for the inner if, and another for the delegate.  For example:

     

    Code Snippet

    if (initializeApplication())

    {

        if (StartUpdateFlow(new EventHandler(delegate(object objVoid, EventArgs voidArgs)

                                                {

                                                    // anonymous method body

                                                })))

        {

            // code when initializeApplication returns true

        }

    }

     

     

     

    What you orignally had was the equivalent of this:

     

    Code Snippet

    EventHandler ev = new EventHandler(delegate(object objVoid, EventArgs voidArgs)

                                           {

                                               // anonymous method body

                                           });

    if (initializeApplication())

    {

        if (StartUpdateFlow(ev))

            ;

    }

     

     

     

    ...and of course, if you removed the semicolon after the call to StartUpdateFlow() you have a syntax error--an if statement with no body.

    Monday, May 26, 2008 2:15 PM
  • This is a reply for the post of Peter Richie ( Whenever i try to reply his post, i get logged out ( talking about bugs Wink )

     

    Peter, your so right! have been way to quick in making my post. When looking at the code it just looked liked the anonymous method was the body of the IF statement. Sorry for ever bothering you with this nonsence and thanks for the help

    Monday, May 26, 2008 5:34 PM