C# 应该“作为字符串”像这样工作吗?
因为字符串在处理对象数组时给我带来了问题。执行后的值显示在注释中。应该这样工作吗?
object[] array = new object[2];
array[0] = 0.33;
array[1] = "0.33";
string a = array[0] as string; // a == null !!!??????
string b = array[1] as string; // b == "0.33"
string a2 = array[0] == null ? "" : array[0].ToString(); // a2 == "0.33"
string a3 = Convert.ToString(array[0]); // a3 == "0.33"
as string caused me a problem when dealing with object arrays. The values after execution are shown in the comments. Should it work this way?
object[] array = new object[2];
array[0] = 0.33;
array[1] = "0.33";
string a = array[0] as string; // a == null !!!??????
string b = array[1] as string; // b == "0.33"
string a2 = array[0] == null ? "" : array[0].ToString(); // a2 == "0.33"
string a3 = Convert.ToString(array[0]); // a3 == "0.33"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,应该的。
as
是强制转换运算符。它只能用于将对象转换为其实际类型(或其超类)。
如果
x
不是Y
,则x as Y
返回null
。Yes, it should.
as
is a cast operator.It can only be used to cast an object to a type that it actually is (or a superclass thereof).
x as Y
returnsnull
ifx
isn't aY
.当 as 运算符无法将对象强制转换为指定类型时,将返回 null。在本例中,它无法将 0.33 转换为字符串类型,因此字符串 a 为空。
the as-operator returns null when it fails to cast an object to the specified type. in this case it failed to cast 0.33 to type string, so string a is null.
MSDN:
所以是的,您观察到的行为是正确的。
MSDN:
So yes, the behaviour you are observing is correct.
as 运算符是强制转换操作,而不是转换操作,因此它只会产生相同类型、超类或子类或您尝试转换为的类型的值。
(与常规转换不同,
as
运算符也不使用operator
关键字执行用户定义的转换。)The as operator is a casting operation, not a conversion operation, so it will only produce the value is the same type, or a super- or sub-class or the type you are trying to cast to.
(Unlike a regular cast, the
as
operator also does not perform user-defined conversions using theoperator
keyword.)“as string”不是“ToString()”的同义词。您正在使用“as”运算符,并且碰巧将其字符串作为类型传递。
as 运算符的定义:
备注
as 运算符类似于强制转换,只不过它在转换失败时生成 null,而不是引发异常。更正式地说,表达式的形式为:
表达式作为类型
相当于:
表达式是类型? (类型)表达式:(类型)null
"as string" is not a synonym for "ToString()". You are using the "as" operator, and happened to pass it string as a type.
Definition of the as operator:
Remarks
The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form:
expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
“as”运算符基本上类似于转换为 System.Type,但唯一的区别是,如果转换失败,它会返回 null 值,而不是引发异常。
查看此链接以获取更多信息
http://msdn.microsoft.com/en-我们/库/cscsdfbt(v=vs.71).aspx
The 'as' operator is basically like casting into a System.Type however the only difference is that it returns a null value if the cast fails instead of throwing an exception.
Check out this link for more info
http://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.71).aspx
array[0]
是一个double
,无法转换为string
,因此null
。调用
ToString()
,大多数类型都会重写它以返回有意义的内容。array[0]
is adouble
and cannot be casted tostring
, hencenull
.Call
ToString()
, most types override it to return something meaningful.