Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何继承string类?_C#_.net_String_Inheritance - Fatal编程技术网

C# 如何继承string类?

C# 如何继承string类?,c#,.net,string,inheritance,C#,.net,String,Inheritance,我想继承以扩展C#string类,添加诸如WordCount()等方法以及其他许多方法,但我一直遇到以下错误: 错误1“WindowsFormsApplication2.myString”:无法从中派生 键入“字符串” 有没有别的办法让我过去?我尝试了string和string,但没有成功。System.string是密封的,所以,不,您不能这样做 您可以创建。比如说, public static class MyStringExtensions { public static int

我想继承以扩展C#string类,添加诸如
WordCount()
等方法以及其他许多方法,但我一直遇到以下错误:

错误1“WindowsFormsApplication2.myString”:无法从中派生 键入“字符串”


有没有别的办法让我过去?我尝试了
string
string
,但没有成功。

System.string是密封的,所以,不,您不能这样做

您可以创建。比如说,

public static class MyStringExtensions
{
    public static int WordCount(this string inputString) { ... }
}
使用:


不能从字符串派生,但可以添加如下扩展:

public static class StringExtensions
{
    public static int WordCount(this string str)
    {
    }
}

你不能继承一个密封类(这就是它的全部要点),它不能同时使用
string
和System.string的原因是关键字
string
只是
System.string
的别名

如果您不需要访问string类的内部,那么您可以创建一个,在您的情况下:

//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;
    }

}
您仍然无法访问string类的私有方法和属性,但在我看来,这比编写:

StringHelper.WordCount(yourString);

LINQ也是这样工作的。

字符串类被标记为
sealed
,因为您不应该从它继承

所以不,你不能“避开它”

您可以做的是在其他地方实现这些功能。可以作为其他类上的普通静态方法,也可以作为扩展方法,使它们看起来像字符串成员


但是,当类被标记为密封时,您无法“绕过”它。

助手类有什么问题?正如错误消息告诉您的,字符串是,所以您当前的方法将不起作用。扩展方法是您的朋友:

myString.WordCount();


static class StringEx
{
    public static int WordCount(this string s)
    {
        //implementation.
    }
}

另一种选择是使用隐式运算符

例如:

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);
    }
}
Foo类的行为类似于字符串

class Example {
    public void Test() {
        Foo test = "test";
        Do(test);
    }
    public void Do(string something) { }
}

如果从string类继承的目的是为了简单,因此代码更具自描述性,那么就不能从string继承。相反,请使用以下内容:

using DictKey = System.String;
using DictValue= System.String;
using MetaData = System.String;
using SecurityString = System.String;
这意味着您的代码现在更加自我描述,并且意图更加明确,例如:

Tuple<DictKey, DictValue, MetaData, SecurityString> moreDescriptive;
Tuple更具描述性;
在我看来,与相同的代码相比,此代码显示了更多的意图,没有别名:

Tuple<string, string, string, string> lessDescriptive;
Tuple-lessdescription;
这种为更多自描述代码设置别名的方法也适用于字典、散列集等


当然,如果您打算向string类添加功能,那么最好使用扩展方法。

如果您不知道什么是扩展方法(我不知道),那么该页面是一个很好的资源。我喜欢您的答案,但不幸的是,这并不是真正针对这个问题的:有了C#元组类型和元组文本特性,您可以用更精确的形式表达这一意图:
(string DictKey、string DictValue、string MetaData)betterTuple并像这样使用它
betterTuple.DictKey=“key”返回d.ToString()需要实现ToString(),或者返回类名。我认为它应该返回d值。有点离题,但有趣的是,如果该示例用于激发在类中存储多个类型对象的灵感,并且实现了更多的运算符来处理实际数据类型与外部世界之间的交换。稍微注意“d”指的是类本身,而d.\u值指的是存储值。如果没有实现ToString(),我们将得到返回类名的默认实现。Brilliant solution。然后可以添加构造验证,一些
==
=
operators、
等于
GetHashCode
方法,并且您有一个sweet封装的value对象。我刚刚使用了上面的解决方案,以及上面提到的其他成员,效果非常好;应返回d.(u)值;或者得到一个对象描述字符串,如GetType()@Omzig,true,它应该是
返回d.\u值。我喜欢这个解决方案。使用此方法,我可以定义自己的“字符串类”,它们在日常使用中充当字符串,但同时也是类型安全的,因此编译器将阻止我将Foo1变量分配给Foo2变量,即使它们都是字符串。
Tuple<string, string, string, string> lessDescriptive;