Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 480
gingerbreadboy Avatar asked Apr 22 '10 11:04

gingerbreadboy


People also ask

What is == and === in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

How can verify if two objects are equal?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)

What is the identical function in R?

identical() function in R Language is used to return TRUE when two objects are equal else return FALSE.

Why does JavaScript have 3 equal signs?

JavaScript === (Triple Equals) The triple equals sign in JavaScript means “equality without type coersion”. That means, the type and values must both be equal. Take for example the scenario where 0 is false. If we compare the same 0 and false with ===, we have false returned.

Which operator check if two values have the same value?

The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions. The result is TRUE if the expressions are equal and FALSE otherwise.

How do you check if two things are equal in R?

The Equality Operator == For example, you can check whether two objects are equal (equality) by using a double equals sign == . We can see if the logical value of TRUE equals the logical value of TRUE by using this query TRUE == TRUE .


2 Answers

The first uses a code block, which will only return a value if you use the return keyword:

value => { return Convert.ToBoolean(value); }

The second, being just an expression doesn't require an explicit return.

like image 177
Marcelo Cantos Avatar answered Sep 19 '22 06:09

Marcelo Cantos


The first one you are not returning anything and you must explicitly return a value since you have a wrapped it, where the second one you are implicitly returning a value.

To fix it do

private static Dictionary<Type, Func<string, object>> _parseActions = new Dictionary<Type, Func<string, object>> 
{ 
  { typeof(bool), value => { return Convert.ToBoolean(value) ;}} 
}; 
like image 29
Robert MacLean Avatar answered Sep 21 '22 06:09

Robert MacLean