Author Sachin Sharma
Published On 18 May 2023
Tags: c#

Here we discuss some best practice for c# development that would help to create good software.

Best Practice for C# Development

1)Naming Conventions

in C# It's very important to follow naming convention for variables,methods and classes.It's make code readable and easy to understand for others.

Naming Conventions for Variables

In c# use variables names as camel cases and name should be meaning full. here we will discuss some good and bad practice.

Good Practice

string userName="Sachin";
int empCode=1234;

Bad Practice

string a="Sachin";
int e=1234;

Naming Conventions for Methods and Class

In c# for Class,Struct,Method and Property name we should use Pascal cases and Methods name should be meaning full so that other developer can understand easily.
Here we will discuss good and bad Practice for method name

Bad Practice

 getEmployee()
 get_employee()

Good Practice

GetEmployee()

2) Use Regions and Comments

For proper code organization we definitely use Regions and proper Comments to seperate logical section of codes and good practice to write only single statement per line. 

#region

Methods block

#endregion

 

3) Use Linq for complex queries

Linq is best to solve complex queries like

// Without LINQ
 

var activeCustomers = new List<Customer>();
foreach (var customer in customers)
{
    if (customer.Status == "Active")
    {
        activeCustomers.Add(customer);
    }
}

// With LINQ

var activeCustomers = customers.Where(c = > c.Status == "Active");

4) Exception Handling

Use exceptional handling to handle exception and error.this help to prevent code from crashing and make it robust.it's good practice to use try catch statement.

try
{
   code block
}
catch(Exception ex){
   handling
}

5)Single Responsibility of Code

Good practice to write multple methods instead of a single method to write multiple functionality.dont write multiple functionality into a single method. Write seperate seperate method for different different functionality.it's help to improve code readability.

Bad Practice
   
 

Class AppNotification
   {
     public void Notification(string type){
     
     if(type=="Recieve")
     {
       // Logic for recieve notification
     }
     
     if(type=="Send"){
      
      // Logic for send Notification
     }   
   }

Good Practice


   class AppNotification{
   
   public void ReveiveNotification(){   
   }   
   public void SendNotification(){     
   }   
   }

5) Use enums for discrete values

Use enums instead of using numbers and string to indicate discrete values. lets understand by example

Bad Practice

public class Logger
{
   public void Print(string message,string logType)
   {
      if(logType=="File"){
      
      }
      else if(logType=="DataBase"){
      
      }
      else if(logType=="Email"){
      
      }
   }
}

Good Practice

public enum LoggerType{
  File,DataBase,Email
}
 
public class Logger{
  public void Print(string message, LogType logType){    
switch (logType){
  case LogType.File:
  console.write("File log");
  break;
      case LogType.DataBase:
  console.write("DataBase log");  
  break;
  case LogType.Email:
  console.write("Email log");  
  break;
}  
  }
}

6)Use string builder instead of string concatination

String is immutable means if any changes happend in string or modified then new object of string is created in memory that impact performance of system. that is reason string builder comes.

StringBuilder is mutable means if any changes or modification happen then
increase the size of string object not create new instance of string.   

Bad Practice

string name="sachin";   
   name +="sharma";

   
Good Practice

StringBuilder sb=new StringBuilder();
   sb.append("sachin");
   sb.append("sharma");

 

I hope that article help you to understand some basic tips for c# development.

Comments

No comments have been added to this article.

Add Comment