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:
我想继承扩展C#字符串类来添加像WordCount()和其他几个方法,但我不断收到此错误:
Error 1 'WindowsFormsApplication2.myString': cannot derive from sealed type 'string'
错误1'WindowsFormsApplication2.myString':无法从密封类型'string'派生
Is there any other way I can get past this ? I tried with string
and String
but it didn't work.
有没有其他方法可以通过这个?我尝试使用string和String但它没有用。
7 个解决方案
#1
36
System.String is sealed, so, no, you can't do that.
System.String是密封的,所以,不,你不能这样做。
You can create extension methods. For instance,
您可以创建扩展方法。例如,
public static class MyStringExtensions
{
public static int WordCount(this string inputString) { ... }
}
use:
string someString = "Two Words";
int numberOfWords = someString.WordCount();
#2
57
Another option could be to use an implicit operator.
另一种选择可能是使用隐式运算符。
Example:
class Foo {
readonly string _value;
public Foo(string value) {
this._value = value;
}
public static implicit operator string(Foo d) {
return d._value;
}
public static implicit operator Foo(string d) {
return new Foo(d);
}
}
The Foo class acts like a string.
Foo类就像一个字符串。
class Example {
public void Test() {
Foo test = "test";
Do(test);
}
public void Do(string something) { }
}
#3
10
If your intention behind inheriting from the string class is to simply create an alias to the string class, so your code is more self describing, then you can't inherit from string. Instead, use something like this:
如果您继承字符串类后的意图是简单地为字符串类创建别名,那么您的代码更自编,那么您就不能从字符串继承。相反,使用这样的东西:
using DictKey = System.String;
using DictValue= System.String;
using MetaData = System.String;
using SecurityString = System.String;
This means that your code is now more self describing, and the intention is clearer, e.g.:
这意味着你的代码现在更加自我描述,意图更清晰,例如:
Tuple<DictKey, DictValue, MetaData, SecurityString> moreDescriptive;
In my opinion, this code shows more intention compared to the same code, without aliases:
在我看来,与相同的代码相比,此代码显示更多的意图,没有别名:
Tuple<string, string, string, string> lessDescriptive;
This method of aliasing for more self describing code is also applicable to dictionaries, hash sets, etc.
这种用于更多自描述代码的别名方法也适用于字典,散列集等。
Of course, if your intention is to add functionality to the string class, then your best bet is to use extension methods.
当然,如果您的目的是为字符串类添加功能,那么最好的办法是使用扩展方法。
#4
4
You cannot derive from string, but you can add extensions like:
您无法从字符串派生,但您可以添加以下扩展名:
public static class StringExtensions
{
public static int WordCount(this string str)
{
}
}
#5
2
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:
助手班有什么问题?正如您的错误消息告诉您的那样,String已被密封,因此您当前的方法将无效。扩展方法是你的朋友:
myString.WordCount();
static class StringEx
{
public static int WordCount(this string s)
{
//implementation.
}
}
#6
1
You can't inherit a sealed class (that's the whole point of it) and the reason why it wouldn't work with both string
and System.String is that the keyword string
is simply an alias for System.String
.
你不能继承一个密封的类(这是它的全部要点)以及它不能同时使用string和System.String的原因是关键字字符串只是System.String的别名。
If you don't need to access the internals of the string class, what you can do is create an Extension Method
, in your case :
如果您不需要访问字符串类的内部,那么您可以创建一个扩展方法,在您的情况下:
//note that extension methods can only be declared in a static class
static public class StringExtension {
static public int WordCount(this string other){
//count the word here
return YOUR_WORD_COUNT;
}
}
You still won't have access to the private methods and properties of the string class but IMO it's better than writing :
您仍然无法访问字符串类的私有方法和属性,但IMO比写入更好:
StringHelper.WordCount(yourString);
That's also how LINQ works.
这也是LINQ的工作方式。
#7
-7
Has it occurred to you that the sealed
keyword isn't just for fun? 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.
但是当一个类被标记为密封时,你不能“绕过”它。
#1
36
System.String is sealed, so, no, you can't do that.
System.String是密封的,所以,不,你不能这样做。
You can create extension methods. For instance,
您可以创建扩展方法。例如,
public static class MyStringExtensions
{
public static int WordCount(this string inputString) { ... }
}
use:
string someString = "Two Words";
int numberOfWords = someString.WordCount();
#2
57
Another option could be to use an implicit operator.
另一种选择可能是使用隐式运算符。
Example:
class Foo {
readonly string _value;
public Foo(string value) {
this._value = value;
}
public static implicit operator string(Foo d) {
return d._value;
}
public static implicit operator Foo(string d) {
return new Foo(d);
}
}
The Foo class acts like a string.
Foo类就像一个字符串。
class Example {
public void Test() {
Foo test = "test";
Do(test);
}
public void Do(string something) { }
}
#3
10
If your intention behind inheriting from the string class is to simply create an alias to the string class, so your code is more self describing, then you can't inherit from string. Instead, use something like this:
如果您继承字符串类后的意图是简单地为字符串类创建别名,那么您的代码更自编,那么您就不能从字符串继承。相反,使用这样的东西:
using DictKey = System.String;
using DictValue= System.String;
using MetaData = System.String;
using SecurityString = System.String;
This means that your code is now more self describing, and the intention is clearer, e.g.:
这意味着你的代码现在更加自我描述,意图更清晰,例如:
Tuple<DictKey, DictValue, MetaData, SecurityString> moreDescriptive;
In my opinion, this code shows more intention compared to the same code, without aliases:
在我看来,与相同的代码相比,此代码显示更多的意图,没有别名:
Tuple<string, string, string, string> lessDescriptive;
This method of aliasing for more self describing code is also applicable to dictionaries, hash sets, etc.
这种用于更多自描述代码的别名方法也适用于字典,散列集等。
Of course, if your intention is to add functionality to the string class, then your best bet is to use extension methods.
当然,如果您的目的是为字符串类添加功能,那么最好的办法是使用扩展方法。
#4
4
You cannot derive from string, but you can add extensions like:
您无法从字符串派生,但您可以添加以下扩展名:
public static class StringExtensions
{
public static int WordCount(this string str)
{
}
}
#5
2
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:
助手班有什么问题?正如您的错误消息告诉您的那样,String已被密封,因此您当前的方法将无效。扩展方法是你的朋友:
myString.WordCount();
static class StringEx
{
public static int WordCount(this string s)
{
//implementation.
}
}
#6
1
You can't inherit a sealed class (that's the whole point of it) and the reason why it wouldn't work with both string
and System.String is that the keyword string
is simply an alias for System.String
.
你不能继承一个密封的类(这是它的全部要点)以及它不能同时使用string和System.String的原因是关键字字符串只是System.String的别名。
If you don't need to access the internals of the string class, what you can do is create an Extension Method
, in your case :
如果您不需要访问字符串类的内部,那么您可以创建一个扩展方法,在您的情况下:
//note that extension methods can only be declared in a static class
static public class StringExtension {
static public int WordCount(this string other){
//count the word here
return YOUR_WORD_COUNT;
}
}
You still won't have access to the private methods and properties of the string class but IMO it's better than writing :
您仍然无法访问字符串类的私有方法和属性,但IMO比写入更好:
StringHelper.WordCount(yourString);
That's also how LINQ works.
这也是LINQ的工作方式。
#7
-7
Has it occurred to you that the sealed
keyword isn't just for fun? 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.
但是当一个类被标记为密封时,你不能“绕过”它。