Why is one Func valid and the other (almost identical) not.

Posted by runrunraygun on Stack Overflow See other posts from Stack Overflow or by runrunraygun
Published on 2010-04-22T11:26:27Z Indexed on 2010/04/22 11:33 UTC
Read the original article Hit count: 255

private static Dictionary<Type, Func<string, object>> _parseActions 
                                   = new Dictionary<Type, Func<string, object>>
    {
        { typeof(bool), value => {Convert.ToBoolean(value) ;}}
    };

The above gives an error

Error 14 Not all code paths return a value in lambda expression of type 'System.Func<string,object>'

However this below is ok.

private static Dictionary<Type, Func<string, object>> _parseActions 
                                   = new Dictionary<Type, Func<string, object>>
    {
        { typeof(bool), value => Convert.ToBoolean(value) }
    };

I don't understand the difference between the two. I thought the extra braces in example1 are to allow us to use multiple lines in the anon function so why have they affected the meaning of the code?

© Stack Overflow or respective owner

Related posts about c#3.0

Related posts about delegates