Skip to main content

Extension Method in C#

Introduction

I would like to share how to create Extension methods in C# and why we need to have them in C#.

First we will discuss the problem and then will rectify the problem using an Extension method.

Understanding the problem

1. Add a Class library as per the following screen. We created a Calculator library.
2. Add the calculator class as per the screen below.


Code
 
public class Calculator
{
    public int Add(int x, int y)
    {
        return x + y;
    }

    public int Subtract(int x, int y)
    {
        return x - y;
    }

    public int Divide(int x, int y)
    {
        return x / y;
    }
}
Understanding the code
  • We created a class library for the calculator
  • The library contains Add, Subtract and Divide methods
  • By mistake the developer forget to add a Multiply method
3. Consuming Library (Creation of client)

We have added a client (console application to consume this library) as in the following
4. Add a refrence of the Calculator library as created above.

5. Add a reference to the class file as per the screen below:
Here in the code above we created an object of the Calculator class and we can see here Multiply method is not available because the developer forget to add it.

So in this situation we have multiple options.

Problem
  • We can raise a request to the third-party DLL company to add the required functionality 
    But it is a long process and it would also affect the cost and delivery of the project because we need to wait until we get a new DLL from the company

    Solution: we can use an Extension method to add a multiply method of this calculator class without modifying the DLL.
6. Addig an Extension class. This should be a static class.

7. We added a Multiply method as per the screen below.


Sample code
 
namespace CalculatorClient
{
    public static class ExtensionClass
    {
        public static int Multiply(this CalculatorThirdPartyLib.Calculator obj, int x, int y)
        {
            return x * y;
        }

    }
}
8. Compile the code.
9. Now as per the screen below we can see the Multiply method is now available to the object of the calculator class.

Conclusion
In this article we learned how to create an Extension method and why we actually need it.

Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Adding CheckBox Column in DataGridView in C# Window Forms

Introduction  I would like to share three ways to add a checkbox to a DataGridView in a C# Windows Forms application. We will learn the following three ways to add a checkbox to a DataGridView: Binding a List to a DataGridView having a bool property Binding a datatable to a DataGridView having a bool column Adding a checkbox column (DataGridViewCheckBoxColumn) to the DataGridView. Procedure 1. Binding a List to a DataGridView having a bool property. Add the following class to the project: public   class  Emp   {       public   bool  isMarried {  get ;  set ; }       public   string  Name {  get ;  set ; }       public   string  city {  get ;  set ; }      }   We have added isMarried as a Boolean property.   Creating a List of Emp c...

Indexes In SQL SERVER in Hindi Part 1