-1

In JavaScript, one can write:

var variableWithALongName = 4;
var amount = 5;

((a) => {
  amount += a * (a + 1);
})(variableWithALongName);

console.log(amount); // 25

The function involved here is an anonymous function, used only in this part of the code, which does not return any value, it just modifies something (it just does some stuff). Instead of that simple function, there could be a more complicated function which takes some complicated arguments and uses them many times.

Is there anything like that in C#?

I am aware of an over 10 years old similar question and I see one of its answers seems to be more useful and more about actual anonymous functions than the official documentation. However, I am interested in functions that don't return any value. The closest to the above JS code I can currently think of is this (e.g. using Unity's Debug.Log):

int variableWithALongName = 4;
int amount = 5;

new Func<int, bool>((a) => {
  amount += a * (a + 1);
  return true;
})(variableWithALongName);

Debug.Log(amount); // 25

However, that function still returns something and, although it is just an (arbitrary) bool value that is never used, I wonder if even that can be avoided, to make that C# code more similar to the JS one.

Also, just to be clear, by anonymous function I think of a function that doesn't take up any name, i.e. that doesn't need something like someName = ... followed by someName(parameters) somewhere else in code. I'm pointing this out because I have noticed that some of these are also being called anonymous functions for reasons unknown to me.

Danijel
  • 123
  • 6
  • 2
    Why not `Action`? It returns `void`. – Wiktor Zychla Aug 25 '20 at 16:53
  • @WiktorZychla I wasn't aware of `Action` as I am relatively new to C#. Searching for anonymous functions in C# mostly results in something that actually takes up a name, while `Func` and `Action` are almost nowhere mentioned, which makes no sense to me. Yes, `Action` answers my question perfectly, thank you! – Danijel Aug 25 '20 at 17:33
  • 1
    @Daniel: remember that these `Action`s or `Func`s are defined somewhere and you can define your own delegate types, too. If you feel you lack a specific function signature, define it then and then use like any other built-in delegate type. – Wiktor Zychla Aug 25 '20 at 17:47

1 Answers1

1

Here is C# equivalent of provided js code

    var variableWithALongName = 4;
    var amount = 5;
    (new Action<int>(a=>  amount += a * (a + 1))).Invoke(variableWithALongName);
    Console.WriteLine(amount);
Serg
  • 3,454
  • 2
  • 13
  • 17
  • What's the difference between `new Action(definition)(parameter);` and `(new Action(definition)).Invoke(parameter);` like in your answer? – Danijel Aug 25 '20 at 17:47
  • 1
    Absolutely no difference in this situation. The Invoke may be helpful in case when you want to call some delegate with checking it for null - so you can something like mydelegate?.Invoke(). You can find more details here: https://stackoverflow.com/questions/1367070/difference-between-delegate-invoke-and-delegate – Serg Aug 25 '20 at 17:53