C# Tips and Tricks (Part 1)

Posted 17/8/2021 by Qasim Asghar
LinkedIn ShareShare

In this first part of a 3 part series, Mohamad Lawand (https://www.mohamadlawand.com/) will be sharing C# tips and tricks you can use.

In this article we will be covering:

  • String interpolation
  • Discards


You can watch the full video on YouTube
 


You can find the source code on GitHub:
https://github.com/mohamadlawand087/v32-CodeTipsTricks-Ep1

String Interpolation

This is a really powerful feature that empower C# developers to be more productive.

The most standard way of outputting text is:
 


What happens now if I want to change the name of the person so instead of Mohamad i want to say "Hello John" or "Hello Mickey".

We have a couple of options to accomplish this

1 - Manually updating the string values with the name that we want, but this is not a flexible way of accomplishing this result

2 - Utilising string formatting so we can do something like this
 


But we can see it has some weird formatting when specifying where the name should be in the string, and for example if we want to add another name we need to do something like this
 


And we can see how it starts to get complicated as we add more information and we try to customise more and more.

The outcome that we want to reach is to make the string output as easy and understandable as possible so what we want is to replace the 0, 1 .... with the actual values that we want to add to the string. So the way we can accomplish this is by string interpolation.

So we want to have something like this
 


But when we run this, we don't get the value we want we get "{name}" instead of the value, thats because string interpolation is not enabled. How do enable it by adding a $ to our string, so it will be something like this
 


The $ tells the compiler that there string interpolation and it needs to get the value of the variables and insert them in the string. So basically we are embedding the variable into our strings.

Now if i have 2 or more variables i can do something like this
 


Basically we can utilise string interpolation any way we see fit in our code.

As well we can extend this by utilising inline methods so lets say i want to make the values upper case for name2 we can do something like this




String interpolation will make our code more readable and easily understandable.

Now lets say i have a more complex scenario of string output that i want to accomplish something like this


 

As we can see we can customise our output very easily, and make the code more readable and easier to maintain.

Now let us see how we can apply some logic inside the string interpolation, so let us say we want to add some conditioning to see if the price of the car was good or not what we can do is following




Adding the ( inside the brace will allow us to execute logic to further customise our output

Now lets say you want to use a brace in our string we can use the double brace so it would be something like this



The double brace will tell string interpolation to ignore the brace.

Default Literal Expressions

We can use the default literal to produce the default value of a type when the compiler can infer the expression type. The default literal expression produces the same value as the default(T) expression where 
T is the inferred type. You can use the default literal in any of the following cases:

  • In the assignment or initialization of a variable.
  • In the declaration of the default value for an optional method parameter.
  • In a method call to provide an argument value.
  • In a return statement or as an expression in an expression-bodied member.

They are enhancements on default value expressions, its a simplifications on how we can utilise it.



For a more advance example we can do something like this:



Discards

They are temporary dummy variables which are read only we cannot write to them, they are useful when we dont need to know the value of the variables.

This will help with saving memory allocation . They enhance readability and maintainability of the code.



Thank you for reading, look out for part 2...

Ready to find your next big challenge? Let's Go!