Cool Tips For A C# Coding Test

Introduction

 
These days whenever we go for an interview, we are often required to take some sort of coding test. This is to give an idea to the employer on how we approach a problem and try to solve it. Even if we do not finish this coding test in the allocated time, we will be analyzed on the design and approach we used. This will help the employer to decide if they want to hire us or not. However, in addition to the design pattern and libraries we use, the syntax and basic code layout is also particularly important to make a good impression. Today, I will list some of the cool tips to make your C# code look better and more professional and impress in a coding test.
 

Cool things to apply in your C# coding test to impress

 
Always create interface before classes
 
This will show that you understand the importance of interface and class relationships and unit testing in your code.
 
Define properties using the new => syntax
 
This is the latest way to allocate values to properties and will show that you are updated on the latest code syntax improvements.
 
Use multiple return values if needed (new tuples).
 
This will show you understand and use the latest features of the coding language.
 
Use class inheritance where needed.
 
This will show you understand the basic principles of object-oriented programming and apply them.
 
Use dependency injection in classes and when creating them use the NEW keyword in the parameters as below.
 
This is a very important point as use of the new keyword is a direct violation of the Dependency Inversion principle which is a core component of the SOLID principles. e.g.:
  1. class Customer: IDiscount, IDatabase {  
  2.     private Ilogger obj;  
  3.     public Customer(ILogger i) {  
  4.         obj = i;  
  5.     }  
  6. }  
  7. IDatabase i = new Customer(new EmailLogger());  
Also, mention that a dependency injection framework can be used e.g. Ninject, Castle Windsor, Unity etc.
 
Create a unit test.
 
Always create a single or multiple unit test to cover the core functionality you are working on. You can use Nunit etc. This will show that you understand the importance of unit testing and TDD (Test driven development).
 
Use the ? operator instead of an IF statement.
 
This will show you understand latest syntax features of the language.
 
Use the ?? operator for checking NULL values.
 
This will show you understand latest syntax features of the language.
 
Use comments.
 
This will show you understand the importance of code comments and documentation.
 
Try to add functions with FUNC delegates for passing function pointers.
 
This will show that you understand delegates which is a very important and strong concept in the C# programming language.
 
Declare class level static variables and use them in functions.
 
This will show you understand scope and nature of declaring and using variables in your code.
 
Avoid loops - use lambda expressions.
 
This will show you understand LINQ, a very powerful and important construct in C# programming.
 
Always use VAR in assignment variables.
 
Using this and the Dynamic keyword will show you understand compile time and runtime type allocations.
 
Re-factor common use code to separate functions.
 
This is a very important thing to remember. Repeating code gives a very bad impression and it shows you do not keep maintainability and extensibility in mind while coding and could be a deal breaker.
 
Re-factor common use code to a separate library if needed across files or projects.
 
Similar to the above point, this is  very important and missing it could be a deal breaker.
 
Create class and initialize properties in one statement.
 
This will show you understand the latest syntax features of the language.
 
Create a constructor with parameters, if required.
 
You can also use properties to assign values to instances of classes, but if you plan to show a class whose value does not change (immutable class) always show assignment through constructors.
 
Finally, remember the SOLID principles and ensure you follow them in any code you write,
  • S -> Single Responsibility Pattern
  • O -> Open Closed Pattern
  • L -> Liskov Substitution
  • I -> Interface Separation
  • D -> Dependency Injection

Summary

 
Although each coding assignment will be different and you might need to apply different patterns and packages, a general layout that showcases your understanding of object-oriented programming and the application of SOLID principles in addition to using the latest language features will give a very good impression on your C# coding test. This list is not final, and many other things can be included in it. However, it can be used as a starting point for a coding test.