C#.Net 3.0或更高版本中的扩展方法

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.

扩展方法使我们能够将方法“添加”到现有类型,而无需创建新的派生类型,重新编译或修改原始类型。 它们是一种特殊的静态方法,但是就像它们是扩展类型上的实例方法一样被调用。 对于用C#编写的代码,在调用扩展方法和实际在类型中定义的方法之间没有明显的区别。

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.

扩展方法必须在静态类中声明才能使它们起作用。 要声明扩展方法,我们需要指定关键字“ this”作为方法的第一个参数。

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.

扩展方法在第一个参数之前具有关键字this。 静态方法的参数声明中没有this关键字。

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.

使用扩展方法时,不会传递使用关键字this声明的参数。 使用静态方法时,不会跳过任何参数。 必须输入所有预期的参数。

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.

扩展方法虽然本质上是静态的,但只能在实例上调用。 试图在一个类上调用它们将导致编译错误。 调用它们的类实例由声明中的第一个参数确定,该参数具有关键字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();
        }
    }
}

翻译自: https://www.experts-exchange.com/articles/2594/Extension-Methods-in-C-Net-3-0-or-more.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值