[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Extension Methods in C#.Net 3.0 or more

Published on
8,672 Points
Last Modified:
Extension methods enable us to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. They are a special kind of static method, but they are called as if they were instance methods on the extended type. For code written in C#, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Extension methods must be declared in Static Classes to make them work. To declare an extension method, we need to specify the keyword "this" as the first parameter of the method.

To Summarize the Extension Method Points:

Extension methods have the keyword this before the first argument. Static methods do not have the this keyword in its argument declaration.
When extension methods are consumed, the argument that was declared with the keyword this is not passed. When static methods are consumed, no arguments are skipped. All expected arguments must be entered.
Extension methods can be defined only in a static class. For static methods, this is not a requirement. Static methods can exist in a regular class as well as in a static class.
Extension methods can be called only on instances values.
It is important to note that extension methods can't access the private methods in the extended type.
If we want to add new methods to a type, we don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
If we create extension methods that have the same signature methods inside the type we are extending, then the extension methods will never be called.
Extension methods cannot be used to override existing methods.
The concept of extension methods cannot be applied to fields, properties or events.
We need to use extension methods sparingly....overuse can be a bad thing!
Extension methods, though static in nature, can be called only on instances. Trying to call them on a class will result in compilation errors. The class instances on which they are called are determined by the first argument in the declaration, the one having the keyword this.


namespace TestExtensionMethods
    public static class MyExtensionMethods
        public static Int32 GetInt32Value(this string iValue)
            Int32 iReturnValue = Convert.ToInt32(iValue);
            return iReturnValue;

namespace TestExtensionMethods
    public class TestClass
        public TestClass()

        private void CheckExtensionValue()
            string sValue = "12";
            int iNewValue = sValue.GetInt32Value();

Open in new window

Author:Umar Topia

Featured Post

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Join & Write a Comment

Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
I previously published an Experts Exchange video Micro Tutorial that describes how to scan documents to a PDF file using an excellent, free product called Foxit Reader: How to scan to a PDF file with free software (https://www.experts-exchange.co…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month