views:

635

answers:

5

Hello,

Is it possible to ha ve a switch in a lambda expression ? IF not, why ? Resharper display it as an error.

+11  A: 

You can in a statement block lambda:

Action<int> action = x =>
{
  switch(x)
  {
    case 0: Console.WriteLine("0"); break;
    default: Console.WriteLine("Not 0"); break;
  }
};

But you can't do it in a "single expression lambda", so this is invalid:

// This won't work
Func<int, int> action = x =>
  switch(x)
  {
    case 0: return 0;
    default: return x + 1;
  };

This means you can't use switch in an expression tree (at least as generated by the C# compiler; I believe .NET 4.0 at least has support for it in the libraries).

Jon Skeet
yes indeed seems the issue is I want an expression tree. Is there any work around better than a if els if o a functino ?
Toto
+2  A: 

Hmm, I see no reason why this shouldn't work. Just be careful with the syntax you use

param => {
    // Nearly any code!
}

delegate (param) {
    // Nearly any code!
}

param => JustASingleExpression (No switches)
Dario
+2  A: 

Yes, it works, but you have to put your code in a block. Example:

private bool DoSomething(Func<string, bool> callback)
{
    return callback("FOO");
}

Then, to call it:

DoSomething(val =>
                {
                    switch (val)
                    {
                        case "Foo":
                            return true;

                        default:
                            return false;
                    }
                });
Brian Genisio
+3  A: 

I checked it too :-)

[Test]
public void SwitchInLambda()
{
    TakeALambda(i => {
        switch (i)
        {
            case 2:
                return "Smurf";
            default:
                return "Gnurf";
        }
    });
}

public void TakeALambda(Func<int, string> func)
{
    System.Diagnostics.Debug.WriteLine(func(2));
}

Works just fine (outputs "Smurf")!

Tor Haugen
+7  A: 

In a pure Expression (in .NET 3.5), the closest you can get is a compound conditional:

    Expression<Func<int, string>> func = x =>
        x == 1 ? "abc" : (
        x == 2 ? "def" : (
        x == 3 ? "ghi" :
                 "jkl")); /// yes, this is ugly as sin...

Not fun, especially when it gets complex. If you mean a lamda expression with a statement body (only for use with LINQ-to-Objects), then anything is legal inside the braces:

    Func<int, string> func = x => {
        switch (x){
            case 1:  return "abc";
            case 2:  return "def";
            case 3:  return "ghi";
            default: return "jkl";
        }
    };

Of course, you might be able to outsource the work; for example, LINQ-to-SQL allows you to map a scalar UDF (at the database) to a method on the data-context (that isn't actually used) - for example:

var qry = from cust in ctx.Customers
          select new {cust.Name, CustomerType = ctx.MapType(cust.TypeFlag) };

where MapType is a UDF that does the work at the db server.

Marc Gravell
You can omit the parentheses in your first code – the result is *much* less ugly and I actually use it sometimes. Admittedly, it takes a bit getting used to but it’s not inherently less readable than an equivalent `switch` block (less so, I’d argue).
Konrad Rudolph
Without the brackets I sometimes get lost in ? / : etc... but yes, it can work without them ;-p
Marc Gravell