I have this C# code snippet which I currently working with.
async Task<string> lol() => "sample string";
var val = await lol();
Console.WriteLine(val);
someone please describe
- what is the meaning of line no 2
CodePudding user response:
I wasn't able to understand that it's a local function
So, you're familiar with methods - you've probably written hundreds of them:
public string GreetMe(DateTime d){
if(d.Hour < 12)
return "good morning";
else
return "good afternoon";
}
You might be familiar with expression bodied methods; they're a single line of code that resolves to a value. They let us skip writing return
, and { }
public string GreetMe(DateTime d) => d.Hour < 12 ? "good morning" : "good afternoon";
You're probably familiar with lambdas:
listOfTimes.Select(t => t.Hour < 12 ? "good morning" : "good afternoon");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lambda's like a mini method, just the logic; the compiler
supplies all the other fluff about types and return keywords
You may be unaware that you can store a lambda (or a normal method, even) in a variable and it can then be passed around to somewhere that will run it. You might also see the word "delegate" used to refer to a variable that holds a method:
Func<DateTime, string> deleg = t => t.Hour < 12 ? "good morning" : "good afternoon";
listOfTimes.Select(deleg);
Or you can run it yourself:
string greeting = deleg(DateTime.Now);
Local functions are kind of like a lambda in that they' embody the logic of some method, but they don't manifest as something that can be passed around - this means they're slightly more performant in some situations because they don't represent anything that needs to be garbage collected
public void SomeMethod(){
//using an expression body
string greetMe1(DateTime d) => DateTime.Now.Hour < 12 ? "good morning" : "good afternoon";
//using a normal body
string greetMe2(DateTime d) {
if(d.Hour < 12)
return "good morning";
else
return "good afternoon";
}
//run the local function, and capture its output
var greeting = greetMe1(DateTime.Now);
}
In short local functions are another way to create an executable unit of code that you might want to call often, as part of a larger item of work. You might be looking for a way to reuse code, or reduce the visual complexity of some massive LINQ statement with a lot of convoluted nested code. They're just like other methods, but are confined to the method in which they're defined. They can also do some things that delegates can't, like call themselves or use yield
, and you might want to be careful where you declare them, because you can declare them at the end of a method and use them "before" they're declared (visually) which is a slight departure from typical C# flow..
CodePudding user response:
async Task<string> lol() => "sample string";
This line is the Task its name is lol and it returns
var val = await lol();
This line take the result of lol()
in this case "sample string" and assigns it to the value val
The reason its called via lol()
and not lol
is this defines that it is a Task/Function and would also allow you to send variables to the Task/Function
An example of this would be lol("New String")