Looking at expression bodied methods, I am trying to understand why the return keyword is not used.
For example with
public int GetAge() { return this.Age; }
I would have expected
public int GetAge() => return this.Age;
as opposed to the correct
public int GetAge() => this.Age;
Why is the return keyword not required/allowed in this case?
Expressions are short methods by convention. It's all about the delegation of a "thing" to do and what it takes to declare the contract: What you have and what you want in return.
It's pretty good to see in Lambdas: While you'll see this most of the time ...
... it is perfectly valid to write ...
... and even:
The latter is a full featured delegate syntax with arguments (
User u) the curly braces for the method scope and the single-line body. In fact, this is quite the same method as this (except the lack of a method name):Which by the way would allow your lambda to look like this because
IsTim()has exactly the same signature as the predicate for the Where-clause (= the lambda determining whether a user is a match or not).Skipping the last example, all of the other three lambda predicates do exactly the same thing but look pretty different:
Coming from case
3to case1over the last years, the .NET team tried to minimize the keystrokes necessary to achieve the same thing. Delegates3have been there forever but with the introduction of Lambdas and Linq, Microsoft needed to simplify the delegating signature which led us to2and1.While case
2is basically exact the same thing as case3, case1is only valid in the correct context. It does not define the type ofubut infers it in the context it is called. If you call this lambda on a list of users,uwill be a user object. If you call it on a list of strings, it's representing a string (and won't compile in this case).It took a while that Microsoft took over this logic to method and even property bodies but the logic is still the same: Why won't you reduce the necessary keystrokes if you can infer the arguments, return types and methods scopes by it's context?
That's why these two methods are exactly the same:
u.Name.StartsWith("Tim")will always return a boolean value and the runtime already knows the arrow=>to split the method arguments from the method body. It really is "pointing" to the expression producing the return value."Pointing to a return value" explains why there should not be a
returnkeyword inpublic int GetAge() => this.Age;.