考虑以下代码:
public class TextType {
public TextType(String text) {
underlyingString = text;
}
public static implicit operator String(TextType text) {
return text.underlyingString;
}
private String underlyingString;
}
TextType text = new TextType("Something");
String str = text; // This is OK.
但如果可能的话,我希望能够做到以下几点。
TextType textFromStringConstant = "SomeOtherText";
我无法使用 TextType 隐式运算符重载扩展 String 类,但是有没有办法将文字字符串分配给另一个类(由方法或其他东西处理)?
String 是一种引用类型,所以当他们开发 C# 时,他们显然必须使用某种方式来获取类的字符串文字。我只是希望它不会被硬编码到语言中。