Phantom Phantom - 1 year ago 100
C# Question

Understadning C# delegate Func<...> operators

I am having some trouble on understanding some C# Func<...> operators, for example, we have the following snippet:

Func<int, bool> a = n => n <= 1;
Func<int, int> b = n => 10;
Func<int, int> c = n => { return n; };
Func<int, int> d = n => a(n)? b(n): n*c(n-1);
Console.WriteLine("{0} {1} {2}", d(1), d(2), d(3));

What is the meaing of
stand for ? is
a(n)? b(n)
stand for a condition ? (e.g. if
is false and b(n) has value, calculate c(n) and return it as
.) I am not sure if I understand this correctly or is my assumption are totally wrong.

mok mok
Answer Source

To make it more clear, you can interpret

n => a(n)? b(n): n*c(n-1);

as (given the definitions provided at your question):

if (n<=1)
    return 10; //b(n)
    return n*n-1; //n*c(n-1)=n*n

Notice that returns are there because of d=...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download