Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Introduction to Writing Better C# Code

Sep 4, 2018 • 5 Minute Read

Introduction

C# has changed from C# 5 to C# 6 and the best standard based practices have also evolved to to make projects more readable. The goal of this series of guides is help you to write cleaner code for your open source C# projects and .NET framework applications that run in a team environment. In such cases, writing good code can be easier for the developers because the code being written is going to be used, managed, and updated by the rest of the developers on the team. Developers who are going to follow the philosophy and coding practices of your individual teams. In these cases, the best approach is to follow the guidelines of the coding teams along with adding design and style to the C# programs in your application projects to make them better for readers. Note that C# compiler doesn't care about the style that you put into the codes, only the performance will be reduced (or increased) by changing the style of the code.

Let’s get deeper into programming C# applications in a way that will make them look simpler, cleaner, and easier for the readers, while at the same time maintaining their performance and efficiency. A few things you should know prior to reading this guide:

  1. Improvements to C# that were made in its 6th version
  2. LINQ in .NET framework
  3. Asynchronous programming and Task object in C#
  4. Unsafe programming in C#, which allows you to go into memory management

Not Focusing on Performance

It should be noted that I will not talking about changing the program performance, upgrading the efficiency, or decreasing the time taken by the programs to run. You can improve program performance in a matter of seconds by writing clean C# code, but the following tips do not guarantee that your code will perform better.

Why Write Clean Code at All?

You write the code, compiler compiles with no warnings and no errors, the code is fine. But what about if some else wanted to read that code out? What if someone had to upgrade the code later for you or the company you worked for? Have a look at the following code:

      public static void Main(string[] args) {
  int x = 0;
  x = Console.Read();

  Console.WriteLine(x * 1.5);
}
    

That program works well, there are no errors in the system, and the application also sort of works. But can you tell me what the program does in real life? Here are some, of many, assumptions that could be made:

  1. It just multiplies the value
  2. It is increasing the value, like in the case of a bonus
  3. It is the interest ratio of the total value of person's bank balance
  4. Etc.

Which one is real? No one will know. In these cases, it is better to write good code and remember to follow the basics of programming. Have a look at the following code:

      public static void Main(string[] args) {
  int salary = 0;
  salary = Console.Read();

  Console.WriteLine(salary * 1.5);
}
    

Doesn't this make more sense than the prior code? We can easily say that this code is going to increase the value of the salary. Note how, just by improving the code, we were able to make sure that someone else could make sense of it much faster than before.

In this guide, I won't show you how to follow best principles. Instead, I will start by building on top of what you already have and teach you how to make the best out of your C# programs. I will focus on how to write good C# logic in your applications, so you will see the many benefits that you can gain from the applications by writing programs in this manner and structure. So, let's get started.

Object Initialization

C# is an object-oriented programming language. What good would it be to write a set of tips if there is no section for objects themselves? This section will focus on what you should consider before moving forward and writing the new Object() code in your applications. You must be aware of how C# classes are created and how things collaborate to bring up a small program in your system. For example, have a look at the following code:

      class Person {
  public int ID { get; set; }
  public string Name { get; set; }
  public DateTime DateOfBirth { get; set; }
  public bool Gender { get; set; }
}
    

You may want to create programs that set the values by default, or have them come from a model, or any other database-oriented data source like this source code, that simplifies the way you enter the defaults in the object at the time the object is being created.

      var person = new Person { ID = 1, Name = "Afzaal Ahmad Zeeshan", DateOfBirth = new DateTime(1995, 08, 29), Gender = true };
    

Instead, try writing the same code in the following way:

      var person = new Person();
person.ID = 1;
person.Name = "Afzaal Ahmad Zeeshan";
// So on.
    

There is no notable performance improvement to the code here, but the readability of the code can really be improved. If you prefer indentation, have a look here:

      var person = new Person
{
  ID = 1,
  Name = "Afzaal Ahmad Zeeshan",
  DateOfBirth = new DateTime(1995, 08, 29),
  Gender = true
};
    

This also has the indentation, but it adds a bit more clarification to the readability to your C# code. Although, these do the same thing, the suggested code makes the code more readable and concise.

Next Steps

Continue on to the next guide in this series for Tips for Writing Better C# Code.