How Can I inherit the string class?

后端 未结 7 2082
春和景丽
春和景丽 2020-12-03 10:03

I want to inherit to extend the C# string class to add methods like WordCount() and several many others but I keep getting this error:

Er

相关标签:
7条回答
  • 2020-12-03 11:03

    What's wrong with a helper class? As your error message tells you, String is sealed, so your current approach will not work. Extension methods are your friend:

    myString.WordCount();
    
    
    static class StringEx
    {
        public static int WordCount(this string s)
        {
            //implementation.
        }
    }
    
    0 讨论(0)
提交回复
热议问题