Real-Time Example Of Extension Method In C#

In most C# interviews, the interviewer asks about a real-time example of extension method. So, here we will have a real time example of extension method for long data types, step by step.
 
This article demonstrates how to create extension method for long data types in C#. A few month ago I came across one of the requirements where I needed to display long data type numbers in a shortened format due to space constraints in the page. The number should be displayed in the page as below with a shortened fomat with suffix k, B and M (K indicates thousands, M - millions and B - Billions). Then I decided to create an extension method for long type and I am going to explain that here. 
 
Requierment is to display numbers as below with suffix K,M,B.
  • 999 then 999
  • 1000 then 1k
  • 12000 then 12k
  • 110000 then 110K 
  • 1120000 then 1.12M
  • 12300000 then 12.3M
  • 123400000 then 123.4M 
  • 1234500000 then 1.235B and so on 
Extension method allows you to add a new method to any existing type/class which were not part of the class. Extension method will help developer to extend the functionality of existing type without creating a new derived type, without changing existing type or without modifying existing type/orignal type.
  
Here I will explain how to create extension method for int type in C# step by step. A few points to remember while creating extension methods,
  • Class shoud be marked as static.
  • Method should be marked as static.
  • First parameter of extension method must be a type which you want to extend. In parameter this keyword is used to bind with what you want to extend.
Step 1
 
Let's start with window console application.
 
Real-Time Example Of Extension Method In C#
 
Step 2
 
Go to Add and click on New project then give project Name as ExtensionMethod and click on ok.
 
Real-Time Example Of Extension Method In C#
 
Step 3
 
Click on add new items and add new class.
 
Step 4
 
Write Extension method for long type. 
  1. public static class LongExtension    
  2.     {    
  3.         public static string FormatNumberKMB(this long num)    
  4.         {    
  5.             if (num >= 100000000000)  
  6.             {  
  7.                 return (num / 1000000000).ToString("0.#B");  
  8.             }  
  9.             if (num >= 1000000000)  
  10.             {  
  11.                 return (num / 1000000000).ToString("0.##B");  
  12.             }  
  13.             if (num >= 100000000)  
  14.             {  
  15.                 return (num / 1000000).ToString("0.#M");  
  16.             }  
  17.             if (num >= 1000000)  
  18.             {  
  19.                 return (num / 1000000).ToString("0.##M");  
  20.             }  
  21.             if (num >= 100000)  
  22.             {  
  23.                 return (num / 1000).ToString("0.#k");  
  24.             }  
  25.             if (num >= 10000)  
  26.             {  
  27.                 return (num / 1000).ToString("0.##k");  
  28.             }  
  29.             if (num >= 1000)  
  30.             {  
  31.                 return (num / 1000).ToString("0.#k");  
  32.             }  
  33.   
  34.             return num.ToString("#,0");  
  35.         }    
  36.     }  
Step 5
 
After adding extension method put logic with static method inside static class. As in the below image all long type numbers are applicable to use the above-created extension method. Once  applied to any long type then this method will be able to be used on long type.
 
Real-Time Example Of Extension Method In C#
 
Step 6
 
How to use long extension method in application.
  1. class Program  
  2.  {  
  3.      static void Main(string[] args)  
  4.      {  
  5.          long number = 999;  
  6.   
  7.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  8.          Console.WriteLine("-----------------------------------");  
  9.   
  10.          number = 1000;  
  11.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  12.          Console.WriteLine("-----------------------------------");  
  13.            
  14.          number = 12000;  
  15.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  16.          Console.WriteLine("-----------------------------------");  
  17.            
  18.          number = 110000;  
  19.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  20.          Console.WriteLine("-----------------------------------");  
  21.            
  22.          number = 1120000;  
  23.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  24.          Console.WriteLine("-----------------------------------");  
  25.            
  26.          number = 12300000;  
  27.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  28.          Console.WriteLine("-----------------------------------");  
  29.            
  30.          number = 123400000;  
  31.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  32.          Console.WriteLine("-----------------------------------");  
  33.   
  34.          number = 1234560000;  
  35.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  36.          Console.WriteLine("-----------------------------------");  
  37.            
  38.          number = 12345670000;  
  39.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  40.          Console.WriteLine("-----------------------------------");  
  41.            
  42.          number = 123456780000;  
  43.          Console.WriteLine(number.ToString() + " Shorten To " + number.FormatNumberKMB());  
  44.          Console.WriteLine("-----------------------------------");  
  45.   
  46.          Console.ReadLine();  
  47.      }  
  48.  }  
Step 7
 
Let's have a look at the final result for the above scenarios.
 
Real-Time Example Of Extension Method In C#
 

Conclusion

 
Extension method allows you to add a new method without creating a new derived type, and without changing existing type. Here I have created long data types. In the same way you can create other types like int, decimal, string etc. I hope this article will help you to understand extension method with real time examples. 


Similar Articles