English speaking and learning Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, August 13, 2017

Extension Method in C#

An extension method enables us to add methods to existing types without creating a new derived type, recompiling, or modify the original types. We can say that it extends the functionality of an existing type in .NET. An extension method is a static method to the existing static class. We call an extension method in the same general way there is no difference in calling.

Essentially, the Extension Method allows us to add a new method to an existing class:

  • Without modifying it or adding code
  • Without extending it or creating a new derived type
  • Without recompiling the class

Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

If you do implement extension methods for a given type, remember the following two points:

  • An extension method will never be called if it has the same signature as a method defined in the type.
  • Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive.

Feature and Property of Extension Methods 

  • It is a static method.
  • It must be located in a static class.
  • It uses the "this" keyword as the first parameter with a type in .NET and this method will be called by a given type instance on the client side.
  • An extension method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement.
  • You can give any name for the class that has an extension method but the class should be static.
  • If you want to add new methods to a type and you don't have the source code for it, then the solution is to use and implement extension methods of that type.

Notes -  The only difference in the declaration between a regular static method and an extension method is the "this" keyword in the parameter list.
Important: If you want the method to receive other parameters, you can include those at the end.

Example 1 - We create an extension method for a string type so string will be specified as a parameter for this extension method and that method will be called by a string instance using the dot operator.
We are creating extension method WordCount .This methos will count the total number of words in a sentence.
Step 1 - Define the namespace
namespace ExtensionExample
Step 2 - Define class as static
    public static class Extension
Step 3 -Define wordcount method as static and pass string preceded with this keyword
        public static int WordCount(this string str)

here is code -
using System;
namespace ExtensionExample
    public static class Extension
        public static int WordCount(this string str)
            string[] userString = str.Split(new char[] { ' ', '.', '?' },
            int wordCount = userString.Length;
            return wordCount;

Step 4 -we will call WordCount method of string
using System;
using ExtensionExample;
class Program
    static void Main(string[] args)
        string userSentance = string.Empty;
        int totalWords = 0;
        Console.WriteLine("Enter the your sentance");
        userSentance = Console.ReadLine();
        //calling Extension Method WordCount
        totalWords = userSentance.WordCount();
        Console.WriteLine("Total number of words is :" + totalWords);


Example 2 - this example uses extension method, 2 parameters.

using System;
static class Extensions
    public static int Addition(this int value, int Addition)
        // Uses a second parameter after the instance parameter.
        return value + Addition;
class Program
    static void Main()
         // 10 + 2=12.
        int result = 10.Addition(2);

Output -  12

Note for Freshers - Please read all my blogs of oops,c#,interview questions to get easily jobs in Software , IT as developer ,programmer.

You may also like

1)Abstraction and Encapsulation in OOPS
2)Inheritance in OOPS
3)Polymorphism in OOPS
4)Interface in OOPS
5)What is Virtual Function
6)What is Abstract class and Abstract function
7)What is Static Class and Static Members
8)What is Collections
9) What is Generics
10)What is Delegate
11)Exception Handling
12)Static Constructor
13)MultiThreading in C#

1)How to make a Registration Page using CAPTCHA in C#

2)How to make a Login Window in C# - Step by Step

3)How to make PhoneBook in C#

4)Insert,Update ,Delete in a C#

2)C# Interview Questions and Answers Part 2

3)C# Interview Questions and Answers Part 3

4)C# Interview Questions and Answers Part 4    

No comments :

Post a Comment