How To Time Saving Shortcuts In C#

It seems like many members here are getting into C#, these are the best 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:

class test_root
{
    public class test_lvl1
    {
        public int a;
    }
    public string somestr;
    public test_lvl1 test;
    public test_lvl1 alternate;
}

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:

test_root r = new test_root();
r.somestr = "whatever";
r.alternate = new test_root.test_lvl1();
r.alternate.a = 5;

if (r.test == null)
    Console.WriteLine(r.alternate.a);

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:
test_root r = new test_root();
r.somestr = “whatever”;
r.alternate = new test_root.test_lvl1();
r.alternate.a = 5;

Console.WriteLine((r.test ?? r.alternate).a);
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:

class test_root
{
    public class test_lvl1
    {
        public class test_lvl2
        {
            public double b;
        }
        public int a;
        public test_lvl2 lvl2_primary;
        public test_lvl2 lvl2_secondary;
    }
    public string somestr;
    public test_lvl1 primary;
    public test_lvl1 alternate;
}

and we’ll initialize it like so

test_root r = new test_root();
r.somestr = "whatever";
r.primary = new test_root.test_lvl1();
r.primary.a = 1;
r.primary.lvl2_secondary = new test_root.test_lvl1.test_lvl2();
r.primary.lvl2_secondary.b = 2.72;
r.alternate = new test_root.test_lvl1();
r.alternate.a = 5;
r.alternate.lvl2_primary = new test_root.test_lvl1.test_lvl2();
r.alternate.lvl2_primary.b = 3.14;

and now, let’s say our code needs to follow this order in getting the .b double value:

  1. Primary value from primary lvl1
  2. Primary value from alternate lvl1
  3. Secondary value from primary lvl1
  4. Secondary value from alternate lvl1

Sure again here we can use if statements, and that might look something like this:

if (r.primary != null && r.primary.lvl2_primary != null)
    Console.WriteLine(r.primary.lvl2_primary.b);
else if (r.alternate != null && r.alternate.lvl2_primary != null)
    Console.WriteLine(r.alternate.lvl2_primary.b);
else if (r.primary != null && r.primary.lvl2_secondary != null)
    Console.WriteLine(r.primary.lvl2_secondary.b);
else if (r.alternate != null && r.alternate.lvl2_secondary != null)
    Console.WriteLine(r.alternate.lvl2_secondary.b);
else
    Console.WriteLine("No valid values");

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.

Console.WriteLine(((r.primary?.lvl2_primary ?? r.alternate.lvl2_primary) ?? (r.primary?.lvl2_secondary ?? r.alternate?.lvl2_secondary))?.b.ToString() ?? "No valid values");

I’m deliberately not going to break that down and explain each component of it, but I will explain the very first bit:

r.primary?.lvl2_primary

this evaluates to something like:

test_root result;
if (r.primary == null)
    result = null;
else
    result = r.primary;

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:

if (int.TryParse(Console.ReadLine(), out int result)) Console.WriteLine(result);

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:

Task.Run(() =>
{
    // code running in the background
    int a = 5;
    int b = 7;
    return a - b;
}).ContinueWith(t =>
{
    if (!t.IsFaulted)
    {
        // running on the UI thread
        Console.WriteLine(t.Result);
    }
}, TaskScheduler.FromCurrentSynchronizationContext());

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.

Task.Run(async () =>
{
    // code running in the background
    Random rng = new Random();
    int a = rng.Next(1, 25), b, sleepfor;
    while ((b = rng.Next(2, 30)) >= a) ;
    sleepfor = rng.Next(a, b);
    await Task.Delay(1000 * sleepfor);
    return (a, b, sleepfor);
}).ContinueWith(t =>
{
    if (!t.IsFaulted)
    {
        // running on the UI thread
        (int a, int b, int sleepfor) = t.Result;
        Console.WriteLine($"a: {a}, b: {b}, slept for {sleepfor} seconds");
    }
}, TaskScheduler.FromCurrentSynchronizationContext());

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:

Console.WriteLine($"a: {a}, b: {b}, slept for {sleepfor} seconds");

is equivalent to

Console.WriteLine("a: {0}, b: {1}, slept for {2} seconds", a, b, sleepfor);

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 leave a like don’t be cheap!

Happy Learning! :+1:

3 Likes

Any Good course to learn C# and asp.net core 2.2. Couldnt decide which one to pick. Need opinion.