Anonymous functions in C#

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

In Javascript, you can create an anonymous function (function literal) in
one statement:

c = function(x,y){return x * y}; // define and assign in one line (how
poetic)
alert ("c = " + c(10,20)); // use it here.

But in C# it seems you need to use 2 lines - one being a delegate - to
accomplish the same thing.

delegate int Multiply(int x, int y); // 1st define and give it
a name (so not anonymous, really).
// Also, it
seems to need to be defined outside of a function
// so global.

Multiply c = delegate(int x, int y) { return x * y; }; // 2nd
define - define the function and assign it
Console.WriteLine("c = " + c(10, 20)); // use it here.

Is this correct?

Just trying to get the similarities straight.

Thanks,

Tom
 
Hi,

yes, its correct. In Net 3.5 you can also use the predefined system
delegate Func<> and write it in two lines:

Func<int, int, int> multiply = (x, y) => x * y;
Console.WriteLine(multiply(10, 20));
 
tshad said:
In Javascript, you can create an anonymous function (function literal) in
one statement:

c = function(x,y){return x * y}; // define and assign in one line (how
poetic)
alert ("c = " + c(10,20)); // use it here.

But in C# it seems you need to use 2 lines - one being a delegate - to
accomplish the same thing.

Well, there has to *be* an appropriate delegate type somewhere, yes. As
the other respondent replied, the Func family of delegates in .NET 3.5
means you very rarely need to declare your own delegate types any more
- but even pre-.NET 3.5 there were some fairly general purpose delegate
types (e.g. EventHandler<T> and Action<T>).
 
In Javascript, you can create an anonymous function (function literal) in
one statement:

    c = function(x,y){return x * y};   // define and assign in oneline (how
poetic)
    alert ("c = " + c(10,20));            // use it here..

But in C# it seems you need to use 2 lines - one being a delegate - to
accomplish the same thing.

        delegate int Multiply(int x, int y);      // 1st define and give it
a name (so not anonymous, really).
                                                              // Also, it
seems to need to be defined outside of a function
                                                              // so global.

        Multiply c = delegate(int x, int y) { return x * y; };  // 2nd
define - define the function and assign it
        Console.WriteLine("c = " + c(10, 20));     // use ithere.

Is this correct?

Just trying to get the similarities straight.

Thanks,

Tom

Hi,
That is true only in the case that a delegate does not exist already.
Remember Javascript is not strong typed. C# is.
 
Ignacio said:
Hi,
That is true only in the case that a delegate does not exist already.
Remember Javascript is not strong typed. C# is.

That makes sense.

So in Javascript you are not defining a type, but in C# you need a type for
the variable you are assigning the function to. The first line then defines
the type (which includes the parameters).

Then you have to redefine the parameters which the actual method code, but
there is no name for the function (method) itself.

Thanks,

Tom
 
Back
Top