Extension Methods

From: http://msdn.microsoft.com/en-us/library/bb383977.aspx

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 written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

From: http://notoriousnerd.blogspot.com/2010/01/getting-started-with-extension-methods.html

Extension methods enables you to hook up extra methods to an already existing (and possibly sealed) class, without the need of subclassing or changing the original class. Lets say I have a class from a third party vendor I use, which contains information on persons

1:  public class Person
2:  {
3:     public string FirstName{ get; set;}
4:     public string LastName{ get; set; }
5:   
6:     //+++
7:  }


However, I might have some situations where I like to generate a quick list of the full names of the persons. I could of course subclass Person and add the following method:

1:  public string GetFullName()
2:  {
3:     return FirstName + " " + LastName; 
4:  }


But this would not be possible if the class was sealed like the String class in C#, and there might be other reasons why you don't want to subclass to add extra methods and functionality. In these cases, we could rather add the GetFullName method as an extension method to the Person class.
A couple of thing to do then:

  • Create a static class to keep the extension method in a namespace called eg: Extensions
  • Add the GetFullName method to this class
  • Make the method static
  • Add a parameter to the method "this Person person"
1:  namespace ExtensionMethods
2:  {
3:     public static class TestingExtensionMethods
4:     {
5:        public static sting GetFullName(this Person person)
6:        {
7:           return person.FirstName + " " + person.LastName; 
8:        }
9:     }
10:  }

To use this extension method on our class operating on Person, we add "using ExtensionMethods;" and then we can use our method as if it was a "normal" method in the Person class:
1:  using ExtensionMethods;
2:   
3:  public class TestClass
4:  {
5:     public TestClass(Person person)
6:     {
7:        string fullName = person.GetFullName();
8:     }
9:  }


Linq contains many useful and often used extension methods which comes into play when we add using System.Linq directive, then eg IEnumerable suddenly supports GroupBy, OrderBy +++.

One final comment: Extension methods cannot access private methods in the class they extend! Extension methods enables you to hook up extra methods to an already existing (and possibly sealed) class, without the need of subclassing or changing the original class. Lets say I have a class from a third party vendor I use, which contains information on persons

1:  public class Person
2:  {
3:     public string FirstName{ get; set;}
4:     public string LastName{ get; set; }
5:   
6:     //+++
7:  }


However, I might have some situations where I like to generate a quick list of the full names of the persons. I could of course subclass Person and add the following method:

1:  public string GetFullName()
2:  {
3:     return FirstName + " " + LastName; 
4:  }


But this would not be possible if the class was sealed like the String class in C#, and there might be other reasons why you don't want to subclass to add extra methods and functionality. In these cases, we could rather add the GetFullName method as an extension method to the Person class.
A couple of thing to do then:

  • Create a static class to keep the extension method in a namespace called eg: Extensions
  • Add the GetFullName method to this class
  • Make the method static
  • Add a parameter to the method "this Person person"
1:  namespace ExtensionMethods
2:  {
3:     public static class TestingExtensionMethods
4:     {
5:        public static sting GetFullName(this Person person)
6:        {
7:           return person.FirstName + " " + person.LastName; 
8:        }
9:     }
10:  }

To use this extension method on our class operating on Person, we add "using ExtensionMethods;" and then we can use our method as if it was a "normal" method in the Person class:
1:  using ExtensionMethods;
2:   
3:  public class TestClass
4:  {
5:     public TestClass(Person person)
6:     {
7:        string fullName = person.GetFullName();
8:     }
9:  }


Linq contains many useful and often used extension methods which comes into play when we add using System.Linq directive, then eg IEnumerable suddenly supports GroupBy, OrderBy +++.

One final comment: Extension methods cannot access private methods in the class they extend!

原文地址:https://www.cnblogs.com/holly/p/1665469.html