C# Tricks & Features

Any programming language has its tricks and purposes.

Gilad Bar Ilan
CodeX
Published in
5 min readJan 6, 2021

--

Understanding the tricks behind any programming language can make your code to be shorter and more efficient.

Expression-bodied members in Classes

In C# OOP we can find some useful tools that can save us a lot of time and place while writing code.

In the following example, we can see two basic declarations of variables.

The declaration of the x & y variable means that we can get & set their values from anywhere.

However, the third declaration means that the variable z will always store the sum of x and y. This means that every time we change the variable x or y the value that z stores will be automatically updated to the current sum of x & y.

By using this expression-bodied members expression we don’t need to use a function or an operation every time we change one of these values.

Note that this operator can be also equal to functions that return values.

In the code above, the value of z will always be the average of x & y.

Here is another example:

Special Keywords

nameof

The following lines of code using a special keyword called nameof. This keyword basically returns the name of the variable you passed into.

In this example the code will print “myVariable”;

goto

Before we begin to talk about the goto keyword, you should know that even tho the goto keyword seemed very unique and useful, it’s important to understand that using it, is not a good practice, but it’s a nice tool to know.

If you are an assembly fan then you most likely already familiar with the goto keyword. The goto keyword it's just like the “jmp” keyword in assembly.

The main idea of the goto keyword is to skip lines of code and jump into a label.

For example, in these lines of code, the line of printing “My Number of 5” will never be executed because the goto keyword skips it and jump to the “nextLabel”.

So the only thing that will be printed on the screen will be the value of the number.

If you know the goto keyword from C for example then you likely think that the following code is legal, however, in C# the goto keyword is different because this code is invalid. After all, the goto keyword cannot be used to jump into an execution block of condition as we see in this example.

ref & out

In C#, we can use the ref & out keywords to change the values of variables that passed as a parameter to the function and update their values outside the function.

To do so, C# offers two different keywords for that, however, what is the difference between them? well, the out keyword accepts the argument but does not take the value of it, we cannot use the value and we must initialize it to some value inside the function, because of that, we can pass to the function a variable that was not initialized as a parameter.

The ref keyword changes the value of the parameter outside the function as well (if you make changes ) but you don't have to initialize the value inside the function and you can’t accept uninitialized variable as a parameter.

When we declare a parameter to be a ref or out we must write in the parameter out/ref then the type and then the variable name for example (out int b).

When we call the function we should write out/ref and the variable name (no type declaration) here is an example of how you should declare in the function and outside the function when we use those keywords:

Note -In case you are not initializing the value of an out parameter inside the function an error will be raised.

Region

We use regions to declare simply what a chunk of code purpose. Regions are a lot like commenting but their declaration and usage are different.

The region here shows that anything that is nestled inside the region represents the sum. Using this way of coding can be useful when we write code and we want to show what the chunk of the code does.

NOTE that all the code inside the Sum would be executed, not like in comments where it’s not.

Nullable<T> & T?

In C# we can make any value type we want to be a nullable type.
Note that reference types are already nullable so we don’t need to do anything for them (the ReferenceType? won’t raise an error but Nullable<ReferenceType> will).

Nullable type means that we can make any type of variable to store null among the other values he can store.

For example, we can make an integer to store null, in addition to the integers that he can already store.

To declare a variable to be nullable we can do it in two different ways.

Congratulations! You now know some of the cool tools that C# can offer.

Hope you enjoyed it :)

--

--