C#函数的参数中含this

函数的参数中含this(扩展方法)

扩展方法必须在非泛型静态类中定义

如下代码

public static class Program
{
    static void Main(string[] args)
    {
        var str = "Test for print string";
        str.PrintString();
        Console.ReadKey();
    }

    public static void PrintString(this string s)
    {
        Console.WriteLine(s);
    }
}

结果如下:

Test for print string
posted @ 2023-01-04 12:57  skc-6  阅读(168)  评论(0)    收藏  举报