<

Extension Methods in C#.Net 3.0 or more

Published on
8,656 Points
2,656 Views
Last Modified:
Approved
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.

Example:-

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

0
Comment
Author:Umar Topia
0 Comments

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Join & Write a Comment

Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
A query can call a function, and a function can call Excel, even though we are in Access. This is Part 2, and steps you through the VBA that "wraps" Excel functionality so we can use its worksheet functions in Access. The declaration statement de…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month