07-28-2019, 05:58 PM
It seems like many members here are getting into C#, and since I use it on a daily basis for work, I figured I'd give you guys examples of some of the cool features you get from C# that you don't get from other languages.
1. Coalesce operator
How many times have you had some nested class or struct, where some of the properties were nullable? Take this one for example:
Now, in your program, you would prefer to get .a from test, but if that doesn't exist, you'd like to get it from alternate. You're probably thinking of writing this:
this works, but it gets exponentially more complicated as you get deeper in levels, I'm sure you can see that. With the coalesce operator though, you don't have to write all of these checks. The following code works the same:
That double question mark is the coalesce operator. What it does is, if the object on the left of it is null, then it will use the object on the right instead.
2. Null-conditional operator
Let's go back to the above example, but this time we'll add some more levels:
and we'll initialize it like so
and now, let's say our code needs to follow this order in getting the .b double value:
but not only is that ugly, it's just a pain in the ass to write...
Now it's time to introduce the null-conditional operator. Whenever you're accessing a member on an object, if you place a question mark before the dot, then C# will stop evaluating at that point if the object is null. What this means is we don't have to do a bunch of error handling, and can just get on with our lives. We can then combine the coalesce operator in such a way that we can reduce this down to the bare minimum number of lines.
I'm deliberately not going to break that down and explain each component of it, but I will explain the very first bit:
this evaluates to something like:
In conclusion, by making use of the null-conditional operator and coalesce operators, we've managed to turn 10 lines of if-elseif-else into 1 single line.
3. inline declarations
Any serious C# programmer isn't running Convert.ToInt32 on user-entered input (or if they are, there's a pile of exception handlers involved). No, we're using int.TryParse. Unfortunately though, that method doesn't return our number, it returns bool. So we have to pass in our desired result as an out parameter. This means we need to waste a line declaring it, right? Well, no. This one is going to be the shortest in this list, but you can actually declare those right in your function call. The following is totally valid code:
4. returning multiple parameters
Have you ever had a lambda function that you're writing and you need to return more than one parameter? It's a lambda, so out parameters are out of the question, you wind up wrapping things into a new class that's just used for that one lambda. Turns out you don't need to do any of that. C# can create a tuple for you on the backend (with as many values as you want), and then deconstruct that into the callee. The best context for this is doing some background work and then returning to the UI thread to update the screen. Let's look at an example:
but what if, instead of doing something like that, we want to generate 2 random numbers, sleep the background thread a random number of seconds between the two numbers, and then return those 3 numbers back so they can be shown on screen? Well, thankfully we actually can do this.
You'll see that simply wrapping our parameters in parenthesis will allow C# to appear as if you're returning multiple values. What you're really getting is Tuple<int,int,int>, but at least this way, you don't have to worry about managing placement, names, etc.
5. String interpolation
And to round this tutorial out, you may have noticed the writeline in the above final example. What does that dollar sign mean? Well, what's written there:
is equivalent to
placing a dollar sign in front of your string tells C# to treat it as if it were String.Format, and replace whatever is within the brackets with it's evaluation.
Well, hope you guys enjoyed, if you did, please let me know.
1. Coalesce operator
How many times have you had some nested class or struct, where some of the properties were nullable? Take this one for example:
Hidden Content
Now, in your program, you would prefer to get .a from test, but if that doesn't exist, you'd like to get it from alternate. You're probably thinking of writing this:
Hidden Content
this works, but it gets exponentially more complicated as you get deeper in levels, I'm sure you can see that. With the coalesce operator though, you don't have to write all of these checks. The following code works the same:
Hidden Content
That double question mark is the coalesce operator. What it does is, if the object on the left of it is null, then it will use the object on the right instead.
2. Null-conditional operator
Let's go back to the above example, but this time we'll add some more levels:
Hidden Content
and we'll initialize it like so
Hidden Content
and now, let's say our code needs to follow this order in getting the .b double value:
- Primary value from primary lvl1
- Primary value from alternate lvl1
- Secondary value from primary lvl1
- Secondary value from alternate lvl1
Hidden Content
but not only is that ugly, it's just a pain in the ass to write...
Now it's time to introduce the null-conditional operator. Whenever you're accessing a member on an object, if you place a question mark before the dot, then C# will stop evaluating at that point if the object is null. What this means is we don't have to do a bunch of error handling, and can just get on with our lives. We can then combine the coalesce operator in such a way that we can reduce this down to the bare minimum number of lines.
Hidden Content
I'm deliberately not going to break that down and explain each component of it, but I will explain the very first bit:
Hidden Content
this evaluates to something like:
Hidden Content
In conclusion, by making use of the null-conditional operator and coalesce operators, we've managed to turn 10 lines of if-elseif-else into 1 single line.
3. inline declarations
Any serious C# programmer isn't running Convert.ToInt32 on user-entered input (or if they are, there's a pile of exception handlers involved). No, we're using int.TryParse. Unfortunately though, that method doesn't return our number, it returns bool. So we have to pass in our desired result as an out parameter. This means we need to waste a line declaring it, right? Well, no. This one is going to be the shortest in this list, but you can actually declare those right in your function call. The following is totally valid code:
Hidden Content
4. returning multiple parameters
Have you ever had a lambda function that you're writing and you need to return more than one parameter? It's a lambda, so out parameters are out of the question, you wind up wrapping things into a new class that's just used for that one lambda. Turns out you don't need to do any of that. C# can create a tuple for you on the backend (with as many values as you want), and then deconstruct that into the callee. The best context for this is doing some background work and then returning to the UI thread to update the screen. Let's look at an example:
Hidden Content
but what if, instead of doing something like that, we want to generate 2 random numbers, sleep the background thread a random number of seconds between the two numbers, and then return those 3 numbers back so they can be shown on screen? Well, thankfully we actually can do this.
Hidden Content
You'll see that simply wrapping our parameters in parenthesis will allow C# to appear as if you're returning multiple values. What you're really getting is Tuple<int,int,int>, but at least this way, you don't have to worry about managing placement, names, etc.
5. String interpolation
And to round this tutorial out, you may have noticed the writeline in the above final example. What does that dollar sign mean? Well, what's written there:
Hidden Content
is equivalent to
Hidden Content
placing a dollar sign in front of your string tells C# to treat it as if it were String.Format, and replace whatever is within the brackets with it's evaluation.
Well, hope you guys enjoyed, if you did, please let me know.