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:

Naming Convention Table:

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

Example:

- Do use PascalCasing for class names and method names.

Public class Employee

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

Public class Employee

- Do not use Underscores in identifiers.

Correct:

- 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

- Do not suffix enum names with Enum.

Avoid:

2)Optimizing Syntax

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

Avoid:

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

Avoid:

- Do object initialization like below:

Avoid:

- Do use null conditional operator like below:

Avoid:

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

Avoid:

- Below is how to use string interpolation.

Test test=new Test();

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

int productSwitch = 1;

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² *