Phantom Phantom - 2 months ago 7
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
?
and
:
stand for ? is
a(n)? b(n)
stand for a condition ? (e.g. if
a(n)
is false and b(n) has value, calculate c(n) and return it as
d
.) I am not sure if I understand this correctly or is my assumption are totally wrong.

mok mok
Answer

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)
else
    return n*n-1; //n*c(n-1)=n*n

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