Introduction
In this article, I am going to share a useful tip on writing clean code that I have learned in C#.
Example
For demo purposes, let's take employees in an organization as an example and create the Employees object which has 3 properties. namely EmployeeID, Name, and Salary.
- class Employees
- {
- public int? EmployeeID { get; set; }
- public string Name { get; set; }
- public long? Salary { get; set; }
- }
Let's consider the use case that we are going to fetch employee details from the database and display or bind these data in front-end.
For demo purposes, I am going to initialize our employee's object with some sample employee details.
- List<Employees> employee = new List<Employees>()
- {
- new Employees() { EmployeeID = 1, Name = "John", Salary = 10000 },
- new Employees() { EmployeeID = 2, Name = "Steve", Salary = 20000 },
- new Employees() { EmployeeID = 3, Name = "Robert", Salary = 30000 }
- };
As you can see, I have created a list of employee objects with three employee's details.
Okay, now we have our data ready. We will consider looping through each employee and display the data in the console.
- foreach (var item in employees)
- {
- if (item != null)
- {
-
- if (item.EmployeeID != null)
- {
- Console.WriteLine(item.EmployeeID);
- }
- else
- {
- Console.WriteLine("No ID Exists");
- }
-
- if (string.IsNullOrEmpty(item.Name))
- {
- Console.WriteLine(item.Name);
- }
- else
- {
- Console.WriteLine("No Name Exists");
- }
-
- if (item.Salary != null)
- {
- Console.WriteLine(item.Salary);
- }
- else
- {
- Console.WriteLine("No Salary Exists");
- }
- }
- }
Here, I have used a traditional way of implementation. As you can easily guess, what I basically did was just loop through each employee and checked for any null reference for the employee object itself and for its properties.
However, if this object has many properties and if you need to have these types of checks for every property then this code becomes more lengthy. Hence this is when the Null Conditional Operator and Null Coalescing Operator comes into the picture. Let me simplify the same logic which was written above using these operators.
- foreach (var item in employees)
- {
- Console.WriteLine(item?.Name ?? "No Name Exists");
-
- Console.WriteLine(item?.EmployeeID.ToString() ?? "No ID Exists");
-
- Console.WriteLine(item?.Salary.ToString() ?? "No Salary Exists");
- }
Isn't this so clear, concise, and yet so easy to implement? Now let me explain what are these operators and how they work.
The ?
operator is the null conditional operator and this operator lets you access members and elements only when the receiver is not-null, returning null result otherwise. In our case, if the item object is null it returns null if not it will print the properties of the item.
The ??
operator is the null coalescing operator and this was designed to be used easily with null-conditional operators. It provides default value when the null conditional operator returns null. Hence in our example, if the item properties are null then the right side of the code going to be printed.
That's it. This is how you can easily use these two operators in combination to make your code look simple and clean.
If you find this post useful, please help others spreading the word. Please leave your suggestions and feedback.