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()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Indexes In SQL SERVER in Hindi Part 1