C# 应该“作为字符串”像这样工作吗?

发布于 2024-11-28 03:13:33 字数 372 浏览 2 评论 0原文

因为字符串在处理对象数组时给我带来了问题。执行后的值显示在注释中。应该这样工作吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

Saygoodbye 2024-12-05 03:13:33

是的,应该的。

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 returns null if x isn't a Y.

2024-12-05 03:13:33

当 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.

罪#恶を代价 2024-12-05 03:13:33

MSDN

as 运算符类似于强制转换操作。然而,如果转换
不可能,因为返回 null 而不是引发异常

所以是的,您观察到的行为是正确的。

MSDN:

The as operator is like a cast operation. However, if the conversion
is not possible, as returns null instead of raising an exception

So yes, the behaviour you are observing is correct.

微暖i 2024-12-05 03:13:33

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 the operator keyword.)

々眼睛长脚气 2024-12-05 03:13:33

“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

滿滿的愛 2024-12-05 03:13:33

“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

幻梦 2024-12-05 03:13:33

array[0] 是一个 double,无法转换为 string,因此 null
调用 ToString(),大多数类型都会重写它以返回有意义的内容。

array[0] is a double and cannot be casted to string, hence null.
Call ToString(), most types override it to return something meaningful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文