Best Practices With If Else Condition In C#

Introduction

In this blog, we will learn the best practice to use if else condition in C#.I have seen more than 10+ projects based on asp.net C# which are developed by experienced developer,s but I think most of the developers are not following best practices of C# coding standards due to which I have decided to write this blog.

Kindly find the screenshot in regards to If else condition. 
 

As per the image, if else condition is used to check if any object value is true or false, because it will take bool value.

By default, if else returns true. 

Syntax
  1. if(booleanExpression) {  
  2.    /* code(s) will execute if the bool condition is true */  
  3. }  
  4. else {  
  5.     /* code(s) will execute if the bool condition is false */  
  6. }  
In this syntax, we can see, if condition is true then if block code will be executed otherwise else block will execute.

See the example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace CsharpCorner  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main()  
  11.         {  
  12.             string result = Calculate(100);  
  13.             Console.Write(result);  
  14.             Console.Read();  
  15.         }  
  16.   
  17.         public static string Calculate(int TotalMarks)  
  18.         {  
  19.             string result = null;  
  20.             if (TotalMarks < 30)  
  21.             {  
  22.                 result = "fail!!!!";  
  23.             }  
  24.             else if (TotalMarks > 30)  
  25.             {  
  26.   
  27.                 result = "Pass";  
  28.             }  
  29.             else  
  30.             {  
  31.                 result = "Need improvement!!!";  
  32.             }  
  33.   
  34.             return result;  
  35.   
  36.   
  37.         }  
  38.     }  
  39. }  
I think instead of these multiple lines of code, we can write only a single line of code.
 
This code can be re-written, as shown below. This is called best practices of C# code .e.g. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace CSharpCornerTest  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main()  
  11.         {  
  12.             string result = Calculate(24);  
  13.             Console.Write(result);  
  14.             Console.Read();  
  15.         }  
  16.   
  17.         public static string Calculate(int TotalMarks)  
  18.         {  
  19.             string result = null;          
  20.   
  21.             result = TotalMarks < 30 && TotalMarks>20   
  22.                      ? "Fail!!!!"   
  23.                      : TotalMarks > 30   
  24.                      ? "Pass"   
  25.                      : "Need improvment";  
  26.             return result;  
  27.   
  28.   
  29.         }
  30.     }  
  31. }  
We can see this can perform same action in a single line of code.
 
Whatever I have explained with you all, we can use in JavaScript, jQuery, Angularjs. etc. In any type of programming language, always try to use best practices. 
 
Advantage of best practice of programming
  • Can reduce line of code. 
  • Easy to understand.
  • Easy to change. 
  • Performance can be improved. 
  • Code will always look clean.
I hope, this blog will be helpful for freshers and professionals.