views:

183

answers:

1

If you are in a method and you pass in an anonymous delegate, does the 'return' key word return a value for the anonymous delegate, or does it return the function? I know in ruby, they use 'next' to achieve this type of functionality inside of a block.

Here's an example:

public bool X()
{
   AList.Where(x => 
    {
       if (x.val == 1) return true;

       ....
       return someBool;
    }
   ...
   return anotherBool
}
+8  A: 

does the 'return' key word return a value for the anonymous delegate, or does it return the function?

It returns from the anonymous delegate.

.Where() doesn't return a bool, you might try .Any()

public bool X()
{
  bool result = AList.Any(x => x.val == 1);
  return result;
}

Or you could use a captured variable:

bool y = false;
Func<int, bool> checker = x =>
{
  if (x == 1)
  {
     return true;
  }
  y = true;
  return false;
}

AList.Where(checker).ToList();
David B