怪奇物语

怪奇物语

首页 新随笔 联系 管理

扩展方法

// 定义一个静态类
public static class StringExtension
{
    // 定义一个静态方法,用this关键字指定要扩展的类型
    public static string MyReverse(this string s)
    {
        // 实现字符串反转的逻辑
        char[] chars = s.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }
}

class Program
{
    static void Main()
    {
        // 调用扩展方法
        string name = "Bing";
        string reversed = name.MyReverse(); // 返回 "gniB"
        System.Console.WriteLine(reversed);
    }
}
posted on 2024-01-23 22:23  超级无敌美少男战士  阅读(31)  评论(0)    收藏  举报