In this tutorial, we will learn about some of the best practices with C# which we should follow to write better code,

Generally, you need to take care of below things when you want to follow coding standards,

1)Naming Conventions:

Naming conventions refers to how we should declare variables.

There are two types which you need to use for naming conventions:

i)PascalCase:The first characters of all words are Upper Case and other characters are lower case.

Example: EmployeeDetails
ii)CamelCase:The first letter is in lowercase and the first letter of every subsequent concatenated word is in caps.Example: employeeDetailsTo declare variable which returns single entity/object use the following convention,var employee=new Employee();To declare variable which return list of entities/objects, one needs to add suffix ‘s’ or ‘List’ to represent it as list of objects,var employees=new Employee();orvar employeeList=new Employee();when you are defining private variable you need to use (_),private string _name=”Vaibhav”;

Naming Convention Table:

- Do Use Meaningful, descriptive words to name variables. Do not use abbreviations.

Example:Correct:String employeeName;int employeeSalary;Avoid:String empname;Int sal;

- Do use PascalCasing for class names and method names.

Public class Employee{Public void AddEmployee(){//..}}

- Do use camel casing for method arguments and local variables.

Public class Employee{Public void AddEmployee(string employeeName,int employeeSalary){//..}}

- Do not use Underscores in identifiers.

Correct:EmployeeInfo employeeInfo;Avoid:EmployeeInfo employee_Info;

- Do prefix interfaces with the letter I.

Public interface IEmployee{//..}

- Do declare all member variables at the top of a class, with static variables at the very top.

public class Employee{Public static string employeeType;Public string employeeName{get;set;};Public int employeeSalary{get;set};Public Employee(){//..}}

- Do not suffix enum names with Enum.

Avoid:Public enum DirectionEnum{//..}

2)Optimizing Syntax

- To declare an empty method that only returns a view in the MVC, we should use the expression body.

Avoid:Public ActionResult Login(){return View();}Correct:Public ActionResult Login() => View();

- To check for null or empty conditions, use the following:

Avoid:var employeeName=”testing”;if(employeeName!=null && employeeName!=””){//..}Correct:var employeeName=”testing”;if(!string.IsNullOrEmpty(employeeName)){//..}

- Do object initialization like below:

Avoid:Test test=new Test();test.id=1;test.name=”value”;Correct:var test=new Test{Id=1;Name=”value”;};

- Do use null conditional operator like below:

Avoid:var employeeName=””;Session[“Name”]=”test”;If(Session[“Name”]!=null){employeeName=Session[“Name”].ToString();}else{employeeName=””;}Correct:var employeeName=””;Session[“Name”]=”test”;employeeName=Session[“Name”]?.ToString() ?? “”

- Avoiding extra braces is also a practice to get into.

Avoid:var count=10;if(count>0){count++;}Correct:Avoid:If(count>0) count++;

- Below is how to use string interpolation.

Test test=new Test();Avoid:var details = string.Format(“{0}, you are welcome, Your Id is {1}”, test.Name , test.Id + “_emp”);Correct:var details = $”{test.Name}, you are welcome, Your Id is {test.Id}_emp”;

- Use of new lightweight switch case introduced in C# 8.

int productSwitch = 1;Good:switch (productSwitch){case 1:Console.WriteLine(“Product 1”);break;case 2:Console.WriteLine(“Product 2”);break;default:Console.WriteLine(“Product case”);break;}Better:var message = productSwitch switch{1 => Console.WriteLine(“Product 1”),2 => Console.WriteLine(“Product 2”)};

Thank You, See you in the next article !!

You can reach out to me here,

LinkedIn: https://www.linkedin.com/in/vaibhav-bhapkar

Email: vaibhavbhapkar.medium@gmail.com

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *