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
}
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
Daniel
  • 16,026
  • 18
  • 65
  • 89
  • 1
    Some one with the rights to edit this should change to title. There is no such thing as C# 3.5. (See this post if you are confused on this issue: http://stackoverflow.com/questions/247621/what-are-the-correct-version-numbers-for-c) – Vaccano Sep 23 '09 at 18:57

1 Answers1

9

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();
Amy B
  • 108,202
  • 21
  • 135
  • 185