Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you assign a function to a variable in C#? [closed]

Tags:

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.

like image 311
Mou Avatar asked Aug 14 '11 09:08

Mou


People also ask

Can you assign a function to a variable?

In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.

Can you assign a function to a variable in C++?

Assigning function to a variable in C++ In C++, assigning a function to a variable and using that variable for calling the function as many times as the user wants, increases the code reusability.

Can you assign a value to a function?

No, you can't assign values to functions.


2 Answers

You can create delegate type declaration:

delegate int del(int number);

and then assign and use it:

   del square = delegate(int x)
    {
        return x * x;
    };

    int result= square (5);

Or as said, you can use a "shortcut" to delegates (it made from delegates) and use:

Func<[inputType], [outputType]> [methodName]= [inputValue]=>[returnValue]

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;

like image 107
Naor Avatar answered Sep 16 '22 13:09

Naor


Func<double,double> square = x => x * x;

// for recursion, the variable must be fully
// assigned before it can be used, therefore
// the dummy null assignment is needed:
Func<int,int> factorial = null;
factorial = n => n < 3 ? n : n * factorial(n-1);

Any of the following more verbose forms is possible, too: (I'm using square as an example):

  • Func<double,double> square = x => { return x * x; };
    The expression is expanded to a statement block.

  • Func<double,double> square = (double x) => { return x * x; };
    Explicit parameter list instead of just one parameter with inferred type.

  • Func<double,double> square = delegate(double x) { return x * x; };
    This one uses the older "anonymous delegate" syntax instead of so-called "lambda expressions" (=>).

P.S.: int might not be an appropriate return type for a method such as factorial. The above examples are only supposed to demonstrate syntax, so modify them as necessary.

like image 45
stakx - no longer contributing Avatar answered Sep 16 '22 13:09

stakx - no longer contributing