铸造与解析 [英] Cast versus parse

查看:151
本文介绍了铸造与解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过关于这个话题但是他们都不正在意义,我的几个相关问题。据我了解,在某些情况下,你可以使用演员和工作交替解析并达到同样的效果。

I've read a few related questions regarding this topic however none of them are making sense to me. As I understand it, in some cases you can use cast and parse interchangeably and achieve the same result.

是否有一些一般准则,可以帮助我决定何时选择其中一个比其他的方法呢?

Are there some general guidelines that can help me decide when to choose one approach over the other?

推荐答案

您通常使用解析()的字符串,其值表示类型的有效价值,这要转换。

You generally use Parse() on a string whose value represents a valid value of the type to which you are converting.

铸造,在另一方面,是更好地使用,当你有一个派生类型的对象,而是存储在一个基础变量,需要使用它为更具体的类型。

Casting, on the other hand, is better used when you have an object of a derived type but stored in a base variable, and need to use it as its more specific type.

也就是说,如果你有1234,你可以解析(),它为一个int。但如果你有

That is, if you have "1234" you can Parse() that into an int. But if you have

object variable = 1234;

您应该投它拿回来为int。

You should cast it to get it back as an int.

这篇关于铸造与解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆