Anonymous methods

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

Can you make an Anonymous methods receive and return values?
Otherwise, they appear useful for void methods?

Thanks
 
Can you make an Anonymous methods receive and return values?
Otherwise, they appear useful for void methods?

Thanks

of course an anonymous method can return a value:
delegate int DoIt();
DoIt d = delegate{ return 2;};
DoIt d1 = () => 12; //now using a lambda expression
int qpp;
qpp = d();
 
Yes:

Predicate<int> isEven = delegate(int i)
{ return i % 2 == 0; }; // C# 2.0
Predicate<int> isEven = i => i % 2 == 0; // C# 3.0

Console.WriteLine(isEven(4));
Console.WriteLine(isEven(5));

Marc
 
Yes:

         Predicate<int> isEven = delegate(int i)
            { return i % 2 == 0; }; // C# 2.0
         Predicate<int> isEven = i => i % 2 == 0; // C#3.0

         Console.WriteLine(isEven(4));
         Console.WriteLine(isEven(5));

Marc


What's Predicate<int>, not familiar with Predicate class.

Thanks
 
What's Predicate<int>, not familiar with Predicate class.

Thanks

It's a predefined delegate. It is defined as :
public delegate bool Predicate<T>

very used in List operations like Find, FindFirst, etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top