C# CS0029 无法将类型 'char' 隐式转换为 'string'
C# CS0029 Cannot implicitly convert type 'char' to 'string'
有人可以解释为什么我不能在没有显式转换的情况下将 char 分配给字符串吗?像这样:
char c = 'a';
string s;
s = c;
Error CS0029 Cannot implicitly convert type 'char' to 'string'
我知道如何将 char 转换为 string,我的问题是为什么编译器不能隐式执行此操作。
是因为char是值类型还是字符串引用还是另有原因?
回答:
同事们,谢谢大家的帮助,经过一番研究,我在这里找到了非常明确的答案:
您需要将字符转换为字符串
char c = 'a';
string s = c.ToString();
或者:
string s = $"{c}";
它们是不同的类型,char 是值类型,string 是引用类型,这就是为什么在 C# 中不能轻易将 char 转换为字符串的原因
每 MSDN
A string is a sequential collection of characters that is used to
represent text. A String
object is a sequential collection of
System.Char
objects that represent a string; a System.Char
object
corresponds to a UTF-16 code unit. The value of the String object is
the content of the sequential collection of System.Char
objects,
and that value is immutable (that is, it is read-only).
char
和 string
类型之间没有转换。 here 描述了实例化字符串的可能方法。 string
是一个包含 char
个元素的序列,你不能直接将简单的项目分配给序列实例,你应该使用 string
ctor 或调用 ToString()
方法
char c = 'a';
string s;
s = new string(c, 1)
char
和 string
类型之间也没有隐式转换,如您在 C# specification
中所见
我知道如何将聊天转换为字符串,我的问题是为什么编译器不能隐式执行此操作。
因为char
类型中没有定义implicit operator string
。
我已经使用 class
而不是 struct
修改了 sample from msdn,它也适用于引用类型
顺便说一句,字符串是引用类型,字符是结构。
public class Digit
{
private readonly byte digit;
public Digit(byte digit)
{
if (digit > 9)
{
throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine.");
}
this.digit = digit;
}
public static implicit operator byte(Digit d) => d.digit;
public static explicit operator Digit(byte b) => new Digit(b);
public override string ToString() => $"{digit}";
}
然后你可以直接将Digit
实例转换为byte
:
var d = new Digit(7);
byte number = d; // here is Digit converted to the byte
Console.WriteLine(number); // output: 7
Digit digit = (Digit)number;
Console.WriteLine(digit); // output: 7
有人可以解释为什么我不能在没有显式转换的情况下将 char 分配给字符串吗?像这样:
char c = 'a';
string s;
s = c;
Error CS0029 Cannot implicitly convert type 'char' to 'string'
我知道如何将 char 转换为 string,我的问题是为什么编译器不能隐式执行此操作。
是因为char是值类型还是字符串引用还是另有原因?
回答:
同事们,谢谢大家的帮助,经过一番研究,我在这里找到了非常明确的答案:
您需要将字符转换为字符串
char c = 'a';
string s = c.ToString();
或者:
string s = $"{c}";
它们是不同的类型,char 是值类型,string 是引用类型,这就是为什么在 C# 中不能轻易将 char 转换为字符串的原因
每 MSDN
A string is a sequential collection of characters that is used to represent text. A
String
object is a sequential collection ofSystem.Char
objects that represent a string; aSystem.Char
object corresponds to a UTF-16 code unit. The value of the String object is the content of the sequential collection ofSystem.Char
objects, and that value is immutable (that is, it is read-only).
char
和 string
类型之间没有转换。 here 描述了实例化字符串的可能方法。 string
是一个包含 char
个元素的序列,你不能直接将简单的项目分配给序列实例,你应该使用 string
ctor 或调用 ToString()
方法
char c = 'a';
string s;
s = new string(c, 1)
char
和 string
类型之间也没有隐式转换,如您在 C# specification
我知道如何将聊天转换为字符串,我的问题是为什么编译器不能隐式执行此操作。
因为char
类型中没有定义implicit operator string
。
我已经使用 class
而不是 struct
修改了 sample from msdn,它也适用于引用类型
顺便说一句,字符串是引用类型,字符是结构。
public class Digit
{
private readonly byte digit;
public Digit(byte digit)
{
if (digit > 9)
{
throw new ArgumentOutOfRangeException(nameof(digit), "Digit cannot be greater than nine.");
}
this.digit = digit;
}
public static implicit operator byte(Digit d) => d.digit;
public static explicit operator Digit(byte b) => new Digit(b);
public override string ToString() => $"{digit}";
}
然后你可以直接将Digit
实例转换为byte
:
var d = new Digit(7);
byte number = d; // here is Digit converted to the byte
Console.WriteLine(number); // output: 7
Digit digit = (Digit)number;
Console.WriteLine(digit); // output: 7