top of page

C# Tricks & Features

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.

public class SampleClass
    public int x { get; set; }
    public int y { get; set; }
    public int z => x + y; //automaticlly holds the sum

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.

public class SampleClass
   public int x { get; set;}
   public int y { get; set;}
   public double z => Avrage(x, y);//automaticlly holds the average   public double Average(int x, int y)
     double sum = x + y;
     return sum / 2.0;

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

Here is another example:

int x = 5;
int y = 6;
int z => x + y; //stores 11 on the beginningpublic void IncX()
 this.x++; /* now x = 6 and z = 12 because we increased x by one and now z is updated to the current sum of x & y which is 12*/

Special Keywords


int myVariable = 5;
Console.WriteLine(nameof(myVariable)); /*prints the name of the variable*/

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”;


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.

int number = 45;
goto nextLabel;
Console.WriteLine("My Number is 5"); //not be executed
  Console.WriteLine(number); //will be executed

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.

int number = 5;
goto myLabel;
if(number == 7)
  Console.WriteLine("I like the goto keyword!");

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.

int b; //not initialized
Initialize(out b); //valid 

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.

int b; //not initialized
Initialize(ref b); //not valid

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:

public class Program
  public static void Main(string[] args)
     int a = 5;
     int b = 6;
     Increase(out b, ref a); //now b = 0, a = 6
     Console.WriteLine(a + " " + b); //prints 6 0
  }public static void Increase(out int b, ref int a)
     a++; // increase the value by 1
     b = 0; //requires initializing the value

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


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.

#region sum int x = 5; int y = 6; Console.WriteLine(x + y); #endregion

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.

int? num = null; //legal
num = 5; //legal

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

Nullable<int> integer = null; //Example 1
int? integer2 = null // Example 2


A cool tool that C# also offers is Action. Action can store chunks of code as their parameter and they can invoke this code over and over as much as they want.

Actions really remind functions but there are some differences, for example, in actions we can take a chunk of code as an argument and invoke it, but the operation on the code in the example below will change the values of variables outside the function because they had been used inside the Action.

int a = 5;
int b = 7;
Action myAct = new Action(() =>{

We can see that we invoked the function once, which means that the values of a & b will be changed once. If we had called the function twice then the chunk of code would be used twice.

This kind of coding style can also help us if we want to make functions that get code as a parameter for example.

At the end of the code (when we call once) the values of the variables will be:

a = 6
b = 8

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

Source: medium

The Tech Platform



bottom of page