Here's some syntactic sugar I've discovered recently and I was quite impressed. Please note it requires C# 7.x (Unity 2019 and newer should be ok).
First, a little background:
A typical method in C# returns one value. Until recently, if you wanted to return two values, like an int and a float, you could either use out parameters, or define a helper struct. But there is other way!
Let's take a look on "old" methods first (if you want to see the new way, it' in 4-th point below).
The problem with out parameters is that returning value via them is a bit less readable than a normal return, and the problem with a helper struct is that is has to be defined. Let's see how it looks like, for reference; and then I'll show you a bit more elegant way.
Let's assume we have our method, getSomeData(), which we would like to return two values: integer age and floating point height.
The first 3 examples below are to show you some context, my "discovery" is in the 4-th point :)
1. out parameter version
The typical way, similar to references from C++: we can "return" our values by out parameters:
Problem with this? Well, it doesn't quite look like returning a value. It looks like calling a method with parameters.
2. out+return version
We can return one value and get other one via out parameter:
The problem with this version is that either of parameters is returned in a different way. If both are equally important, it looks strange (if not deceptive).
3. Helper struct
We can define a helper struct and return it:
This version is very clean and readable when we define and call out method, but...
... before that it requires us to create a whole new structure, just for returning two values at once.
4. Sugar in C#7!
So it seems... We can just return two values at once!
No comments:
Post a Comment