使用扩展方法需要注意的几点:

    1.  The method is define in a top level static class ( the class is directly under the namespace)
    2. The method is static and decorates its first param with a new param modifier this, this param is called the "instance parameter" and its an error to use the "this" modifiers on any other parameter.
    3. No other parameter modifiers ( ref, out etc) are allowed with "this" (so values types can't be passed by reference to an extension methods, VB will allow ref).
    4. The instance parameter can't be a pointer type.
    5. The method is public (its accessible to anyone who can get to its parentclass).
    6. The Type parameter used must be defined on the method and not the parentclass.
    7.  The instance parameter cannot have the type of the Type parameter.

Sample:

 1C# 中的扩展方法---Extension methods in C#using System;
 2C# 中的扩展方法---Extension methods in C#using System.Collections.Generic;
 3C# 中的扩展方法---Extension methods in C#using System.Linq;
 4C# 中的扩展方法---Extension methods in C#using System.Text;
 5C# 中的扩展方法---Extension methods in C#
 6C# 中的扩展方法---Extension methods in C#namespace NewFeatures
 7



参阅 :Sree's ventures Extension method in C#

相关文章:

  • 2021-09-24
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
  • 2021-10-30
  • 2022-12-23
  • 2021-08-18
  • 2021-10-25
猜你喜欢
  • 2021-10-11
  • 2022-02-08
  • 2021-07-12
  • 2021-11-30
  • 2022-12-23
  • 2021-05-29
相关资源
相似解决方案