How Can I inherit the string class?

后端 未结 7 2128
春和景丽
春和景丽 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 10:54

    The string class is marked sealed because you are not supposed to inherit from it.

    So no, you can't "get around it".

    What you can do is implement those functions elsewhere. Either as plain static methods on some other class, or as extension methods, allowing them to look like string members.

    But you can't "get around" it when a class is marked as sealed.

提交回复
热议问题