i saw a function can be define in javascript like
var square = function(number) {return number * number};
and can be called like
square(2);
var factorial = function fac(n) {return n<3 ? n : n*fac(n-1)};
print(factorial(3));
c# code
MyDelegate writeMessage = delegate ()
{
Console.WriteLine("I'm called");
};
so i need to know that can i define a function in the same way in c#. if yes then just give a small snippet of above like function definition in c# please. thanks.
You can create delegate type declaration:
and then assign and use it:
Or as said, you can use a “shortcut” to delegates (it made from delegates) and use:
for example:
Func<int, int> square = x=>x*x;int result=square(5);You also have two other shortcuts:
Func with no parameter:
Func<int> p=()=>8;Func with two parameters:
Func<int,int,int> p=(a,b)=>a+b;