c# - 如何扩展 String 类?

标签 c# extending-classes

在 javascript 中扩展核心类非常容易。我的印象是在 C# 中并不那么容易。我想向 String 类添加一些东西,这样我就可以做类似的事情:

string s = "the cat's mat sat";
string sql = s.smartsingleQuote();

因此给了我

the cat''s mat sat

这是否可行,还是我必须为此编写一个函数?

最佳答案

是的,可以使用扩展方法 - MSDN

这是一个示例代码。

public static class Extns
{
    public static string smartsingleQuote(this string s)
    {
        return s.Replace("'","''");
    }
}

免责声明:未经测试。

关于c# - 如何扩展 String 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18371654/

相关文章:

c# - 动态更新事件

c# - 使用 Where 子句在 DataTable 的列中查找最小/最大值?

c++ - MSVC++ 扩展 iostream 时出错

java - 创建任务[]任务数组

php - 未找到 codeigniter MY_Controller

c# - 将数字映射到字母

c# - 转换为 lambda 表达式

c# - 使用 Entity Framework Core 存储计算属性

android - 具有相同设计的延伸微调器

ios - 将 UIViewController 实现拆分为多个源文件