转换和解析之间的区别
我已经研究一些代码有一段时间了。现在我只是按原样使用关键字,而没有真正理解它们。所以这是我的问题
铸造和解析之间有什么区别?
UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String
-->这是铸造还是解析?
(字符串)UserAdapter.GetIdAndUserTypeByEmailAndPassword(电子邮件,密码).Rows[0][“UserType”]
--> 这是强制转换还是解析?
UserAdapter.GetIdAndUserTypeByEmailAndPassword(电子邮件、密码).Rows[0]["UserType"].ToString()
x.ToString()
和 (String) x
有什么区别?
I have been working on some code for a while. for now i have just used the keywords as-is without actually understanding them. So here is my question
What is the difference between Casting and Parsing?
UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String
--> is this Casting or parsing?
(String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"]
-->is this Casting or parsing?
UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"].ToString()
What is the difference bewtween x.ToString()
and (String) x
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些是无关的。
强制转换就是改变变量的类型。
解析是“检查”字符串并将其逻辑值分配给某个变量。
(补充:嗯,它们在某种意义上是相关的,因为从很远的地方来看,两者都可以用于“转换”数据,但是,数据只有在解析时才会真正转换)
这是一种特殊的转换,如果类型不可转换,它也不会失败(请参阅 这里),但会让你
null
。这又是强制转换,但如果表达式不是
string
类型,则会抛出异常。x.ToString()
将尝试对对象 x 调用 ToString()。(String) x
将尝试将 x 转换为字符串,如果 x 不是字符串,则会失败。Those are unrelated.
Casting is changing the type of the variable.
Parsing is 'examining' the string and assigning its logical value to some variable.
(ADDITION: Well, they are related in a sense, because from far far away both can serve to 'convert' data, however, data is really converted ONLY in case of parsing)
This is special kind of casting that will not fail if types aren't convertible (look here), but will get you
null
.This again is casting, but will throw an exception if expression isn't of type
string
.x.ToString()
will try to call ToString() on the object x.(String) x
will try to cast x to string, and will fail if x isn't string.区别如下:
上面的代码将打印 false,因为我们正在进行类型转换(只是更改数据类型)。
现在上面的代码将打印 true,因为我们正在解析(逻辑设置)值。
那么有什么区别(请参阅此示例):
正确方法:
为什么,因为 char 只能存储 1 个字符(当然!)。
摘要-类型转换只是更改数据类型,而解析意味着逻辑地设置值并生成预期结果,而不是这样。
HERE IS THE DIFFERENCE:
Above code will print false, because we are typecasting (just changing the data type).
Now above code will print true because we are parsing (logically setting) value.
SO WHAT IS THE DIFFERENCE (see this example):
Correct way :
Why because char can store only 1 character (ofcourse!).
SUMMARY - typecasting is just changing the datatype WHILE parsing means logically setting the values and generate expected result not like this .
对于击球手的想法,请检查下面的链接。
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/57518616-a142-4e89-99c9-e2fa6b01ef6f
for the batter idea please check the below link.
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/57518616-a142-4e89-99c9-e2fa6b01ef6f