C#参数列表中的this(扩展方法)

  1. 参数列表中this的这种用法是在.NET 3.0之后新增的一种特性---扩展方法。通过这个属性可以让程序员在现有的类型上添加扩展方法(无需创建新的派生类型、重新编译或者以其他方式修改原始类型)。
  2. 扩展方法是一种特殊的静态方法,虽然是静态方法,但是可以像使用类型上的实例方法一样去使用扩展方法(调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异)。
  3. 扩展方法的第一个参数指定该方法作用于哪个类型,并且该参数以this修饰符为前缀。
  4. 当你在其他命名空间使用时,需要用using将扩展方法所在命名空间显式导入到源代码中。
  5. 扩展方法必须在非范型静态类中定义。
  6. 扩展方法无法访问扩展类型中的私有变量。
  7. 可以使用扩展方法来扩展类或接口,但不能重写扩展方法。
  8. 与接口或类方法具有相同名称和签名的扩展方法永远不会调用(这一点要牢记)。编译时,扩展方法的优先级总是比类型本身中定义的实例方法低。换句话说,如果某个类型具有一个名为Process(int i)的方法,而你有一个具有相同签名的扩展方法,则编译器总是绑定到该实例方法。当编译器遇到方法调用时,它首先在该类型的实例方法中寻找匹配的方法。如果未找到任何匹配方法,编译器将搜索为该类型定义的任何扩展方法,并且绑定到它找到的第一个扩展方法。
  9. 通常建议你只在不得已的情况下才实现扩展方法,并谨慎地实现。只要有可能,必须扩展现有类型的情形都应该通过创建从现有类型派生新类型来达到这一目的。
  10. 在使用扩展方法来扩展你无法更改其源代码的类型时,你需要承受该类型实现中的更改会导致扩展方法失效的风险。
  11. 在代码中,可以使用实例方法语法调用扩展方法。但是,编译器生成的中间语言(IL)会将代码转换为对静态方法的调用。因此,并未真正违反封装原则。实际上,扩展方法无法访问他们所扩展的类型中的私有变量。

参考资料:

https://zhidao.baidu.com/question/263333215940019885.html

http://www.php.cn/csharp-article-352532.html

https://www.cnblogs.com/jhxk/articles/1796382.html

又看到一篇不错的博客:

https://www.cnblogs.com/zhaopei/p/5678842.html

以下为本人调试时的代码:

代码1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 参数列表中的this
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "888";
            str = str.ChenWei(999).ToString();
            Console.WriteLine(str);

            MyClass mc = new MyClass();
            string info = mc.lol(999);
            Console.WriteLine(info);

            Console.ReadLine();
        }
    }

    public static class MyExtensions//扩展方法必须在非泛型静态类中定义
    {
        //为string添加扩展方法;第一个参数指定该方法作用于哪个类型,并且该参数以this修饰符为前缀
        public static object ChenWei(this string str, int age, string tmp = "www")//扩展方法必须是静态的,“this”参数修饰符必须在第一个参数上
        {
            return "chenwei---15601598187" + "---" + str + "---" + age + "---" + tmp;
        }

        //为自定义类型添加扩展方法;第一个参数指定该方法作用于哪个类型,并且该参数以this修饰符作为前缀
        public static string lol(this MyClass mc, int age)//扩展方法必须是静态的,“this”参数修饰符必须在第一个参数上
        {
            return "110" + "---" + mc.Info() + "---" + age;
        }
    }

    public class MyClass
    {
        public string name = "xiechen";
        public int age = 1;

        public string Info()
        {
            return name + ":" + age;
        }
    }
}

代码2:

using System;

//Define an interface named IMyInterface.
namespace DefineIMyInterface
{
    public interface IMyInterface
    {
        //Any class that implements IMyInterface must define a method
        //that matches the following signatures.
        void MethodB();
    }
}

代码3:

using System;
using DefineIMyInterface;

//Define extension methods for IMyInterface
namespace Extensions
{
    //The following extension methods can be accessed by instances of any class that implements IMyInterface.
    public static class Extension
    {
        public static void MethodA(this IMyInterface myInterface, int i)
        {
            Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, int i)");
        }

        public static void MethodA(this IMyInterface myInterface, string s)
        {
            Console.WriteLine("Extension.MethodA(this IMyInterface myInterface, string s)");
        }

        //This method is never called in ExtensionMethodsDemo1,
        //because each of three Class A,B and C implements a method named MethodB
        //that has a matching signatures.
        public static void MethodB(this IMyInterface myInterface)
        {
            Console.WriteLine("Extension.Method(this IMyInterface myInterface)");
        }
    }
}

代码4:

using System;
using Extensions;
using DefineIMyInterface;

//Define three classes that implement IMyInterface, and then use them to test
//the extension methods.
namespace ExtensionMethodsDemo1
{
    class A : IMyInterface
    {
        public void MethodB()
        {
            Console.WriteLine("A.MethodB()");
        }
    }

    class B : IMyInterface
    {
        public void MethodB()
        {
            Console.WriteLine("B.MethodB()");
        }

        public void MethodA(int i)
        {
            Console.WriteLine("B.MethodA(int i)");
        }
    }

    class C : IMyInterface
    {
        public void MethodB()
        {
            Console.WriteLine("C.MethodB()");
        }

        public void MethodA(object obj)
        {
            Console.WriteLine("C.MethodA(object obj)");
        }
    }

    class ExMethodDemo
    {
        static void Main(string[] args)
        {
            //Declare an instance of class A, class B and class C
            A a = new A();
            B b = new B();
            C c = new C();

            //For a,b and c, call the following methods:
            //MethodA with an int argument
            //MethodA with a string argument
            //MethodB with no argument.

            //A contain no MethodA, so each call to MethodA resolves to
            //the extension method that has a matching signatures.
            a.MethodA(1);//Extension.MethodA(object, int)
            a.MethodA("hello");//Extension.MethodA(object, string)

            //A has a method that matches the signature of the following call to MethodB
            a.MethodB();//A.MethodB();

            //B has methods that match the signatures of the following method calls.
            b.MethodA(1);//B.MethodA(int)
            b.MethodB();//B.MethodB()

            //B has no matching method for the following call,but class Extension does.
            b.MethodA("hello");//Extension.MethodA(object, string)

            //C contains an instance method that matches each of the following method calls.
            c.MethodA(1);//C.MethodA(object)
            c.MethodA("hello");//C.MethodA(object)
            c.MethodB();//C.MethodB()

            Console.ReadLine();
        }
    }
}

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#扩展方法是一种特殊的静态方法,它允许您向现有的类型添加新的方法,而无需修改原始类型的定义。通过扩展方法,您可以在不创建新的派生类型或重新编译代码的情况下,为现有类型添加功能。 扩展方法的定义方式如下: ```csharp public static class ExtensionClass { public static ReturnType ExtensionMethod(this ExtendedType instance, parameters) { // 扩展方法的实现 } } ``` 其,`ExtensionClass`是包含扩展方法的静态类,`ExtensionMethod`是扩展方法的名称,`ExtendedType`是要扩展的类型,`instance`是该类型的实例,`parameters`是方法参数,`ReturnType`是方法的返回类型。 通过扩展方法,您可以像调用实例方法一样调用它们,而无需创建该类型的实例。例如,假设您想为字符串类型添加一个方法来反转字符串,可以使用扩展方法实现如下: ```csharp public static class StringExtensions { public static string Reverse(this string str) { char[] charArray = str.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } } ``` 然后,您可以在代码使用该扩展方法: ```csharp string myString = "Hello World"; string reversedString = myString.Reverse(); // 调用扩展方法 Console.WriteLine(reversedString); // 输出:dlroW olleH ``` 扩展方法C#非常有用,可以提高代码的可读性和灵活性,同时避免了修改原始类型的定义。它们经常用于扩展.NET框架的基本类型,也可以用于自定义类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值