Princy Gupta
What are extension mehtods
By Princy Gupta in .NET on Apr 18 2014
  • Princy Gupta
    Apr, 2014 18

    http://www.codeproject.com/Articles/34209/Extension-Methods-in-C

    • 2
  • Rajendra Tripathy
    Aug, 2014 8

    Definition : An extension method has simplified calling syntax. It represents static methods as instance methods. An extension method uses the this-keyword in its parameter list. It must be located in a static class.Example ::this class has a field for the ProductName and for the ProductPrice.namespace Products {public class Product{public string ProductName { get; set; }public double ProductPrice { get; set; }} }No problems so far, so you use this class in a list and display the name and price:class Program{static void Main(string[] args){List Products = new List(){new Product{ProductName = "White Shoe", ProductPrice = 10},new Product{ProductName = "Green Shoe", ProductPrice = 5},new Product{ProductName = "Black Shoe", ProductPrice = 15}};foreach (Product p in Products){Console.WriteLine("{0} costs {1}", p.ProductName, p.ProductPrice);}}}Now you realize it would be great to display how much the VAT (tax) is of the price as well. However, you do not own or have control over the Product class since it is owned by someone else. So you can't add a CalculateVat() method to the product class. You could of course, in code above, display it like so:foreach (Product p in Products){Console.WriteLine("{0} costs {1} (VAT: {2})", p.ProductName, p.ProductPrice, (p.ProductPrice * .25));}This however requires you to do it everywhere you display this, and if the VAT changes, you have to edit the value everywhere, so it would be much nicer to have this as a method on the Product class directly instead. Enter the Extension method.This is as simple as this, define a static class, declare a static method that does what you want to do. The method should take as an argument the class we want to add the method to and it has to be prefixed with the THIS keyword. Like so:public static class MyExtensions{public static double CalculateVat(this Product p){return (p.ProductPrice * .25);}}That is all that is needed, we can now call the Extension method :foreach (Product p in Products){Console.WriteLine("{0} costs {1} (VAT: {2})", p.ProductName, p.ProductPrice, p.CalculateVat());}

    • 1
  • Bipin Sethi
    Mar, 2019 31

    Faced the similar question for Microsoft interview recently. http://www.codeproject.com/Articles/34209/Extension-Methods-in-C

    • 0
  • sanjib kotal
    Jul, 2016 9

    Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

    • 0
  • sanjib kotal
    Jul, 2016 9

    Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

    • 0
  • JK RAO
    Jan, 2015 29

    Simple and Clear cut Answer. Thank Q.

    • 0
  • Vijay Kumar
    Sep, 2014 5

    Extension methods are a new feature in C# 3.0. 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. Feature and Property of Extension Methods The following list contains basic features and properties 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. It also shown by VS intellisense. When we press the dot (.) after a type instance, then it comes in VS intellisense. 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. If you create extension methods that have the same signature methods as the type you are extending, then the extension methods will never be called.

    • 0
  • Munesh Sharma
    Apr, 2014 22

    http://www.c-sharpcorner.com/uploadfile/puranindia/extension-methods-in-C-Sharp-3-0/

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS