In JavaScript I can assign a value to a variable by dynamically creating a function. Such as
var name = (function () { name="bob"; return name; }());
I'm fairly certain that with C# 4.0 the same type of thing is possible. Could someone show me the syntax of how the same line above would look in C#?
Also, if you could jog my memory on what the proper term for creating this type of dynamic function is, it would be much appreciated!
Thanks for your help!
PS: It's likely this question has been asked before, but since I was unclear on the nomenclature I may have missed finding it. If that's the case, I apologize!
Dynamic function calls are useful when you want to alter program flow according to changing circumstances. We might want our script to behave differently according to a parameter set in a URL's query string, for example. We can extract the value of this parameter and use it to call one of a number of functions.
Dynamic function is a way of dynamically invoking a function call. The compiler will have limited knowledge of what you are up to so you will get run time errors if you don't use correct inputs and outputs. One example that runs different functions depending on user input: DEFINE VARIABLE iFunc AS INTEGER NO-UNDO.
Creating The Dynamic Function The Function object can also be used as a constructor function to create a new function on the fly. The syntax for creating a function from Function Object is as follows: const myFunction = new Function(arg1, arg2, … argN, body);
In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.
You can use anonymous methods:
Func<string> anonymousFunction = () => { string name = "bob"; return name; };
string myName = anonymousFunction();
The syntax on the first line is a lambda, which is the C#3.0 and above way of declaring anonymous methods. The above function takes no arguments, but there's nothing stopping you from including them as well:
Func<string, string> makeUppercase = x => x.ToUpper();
string upperCase = makeUppercase("lowercase");
Note that since there is only one parameter, you can elide the brackets around it. As well, since the entire method is a single return statement, you can elide both the brace brackets as well as the return
statement itself.
This type of lambda is very common when using the LINQ extension methods, since many of them require a single-argument method that returns a value:
var numbers = new List<int>() { 1, 2, 3, 4 };
var divisibleByTwo = numbers.Where(num => num % 2 == 0);
To answer your actual question, that syntax is not valid in C#. If you try this:
string output = (x => x.ToUpper())("lowercase");
You'll get an error message saying "Method name expected." You have to assign the anonymous method to a delegate first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With