何时投射,何时解析以及何时转换? [英] When to cast , when to parse and when to convert ?

查看:95
本文介绍了何时投射,何时解析以及何时转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii团队,





我对三件事情的要求非常困惑

什么时候到演员,何时解析以及何时转换?



例如



(int)obj.value ;



int.parse(obj.value);



convert.toint32(obj.value) ;



请告诉我何时使用这些东西,以及这三者之间的实际差异是什么

解决方案

< blockquote>当你有一个包含值表示的字符串时解析。例如,当您有用户将数据输入TextBox并需要将其用作数字时:

  int   value  =  int  .Parse(myTextBox.Text); 



或者最好使用TryParse:

  int  ; 
if int .TryParse(myTextBox.Text, out value ))
{
...使用 value
}
else
{
...向用户报告输入错误
}





转换相当于Parse for strings,但它没有TryParse版本,而且它稍慢,因为它必须决定什么键入参数是:所以如果你有一个对象值转换为 int Convert.ToInt32将整理出来类型并且这样做。



Casting不做同样的工作:它是我知道这是什么,只是这样做的语法糖。 - 它不会改变实际的对象,它只是改变它的类型(除了少数例外,例如从double转换为int:截断double并抛弃小数部分)。但它不会更改对象类型,除非它们是可转换的:这意味着将派生类型转换为基类型,或者如果存在显式转换运算符(例如 double - > int 等等。你不能使用强制转换将字符串更改为int!



所以:

1)如果你有一个字符串,请使用Parse,或TryParse(或TryParseExact)

2)如果你有一个对象并且你不一定知道它是什么类型,请使用转换

3)如果你这样做知道它是什么类型,只需要以不同的方式引用它,投射它。


如果你有一个 int你需要强制转换存储在对象 byte short ...例如,如果您在会话变量中存储了 int ,那么您需要执行:

  int  yourInt =( int )会话[  yourInt]; 



对于之间的区别int.Parse Convert.ToInt32 ,请参阅:

http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and -convert-toint32 [ ^ ]

如果您有字符串,请使用以下方法之一,这只是一个整数表示为字符串,例如:

  string  str =   100; 
int i = Convert.ToInt32(str); // 或int.Parse
// 只有在100%确定可以将字符串转换为int时才这样做。如果您不是100%确定,请使用int.TryParse



如果您有用户输入(或者如果您不是100%确定可以转换为字符串到int),那么你既不应该使用转换,也不应该使用 int.Parse ,也不要使用 Convert.ToInt32 。您需要使用 int.TryParse [ ^ ],因为 TryParse 首先检查是否可以将其解析为int:

  string  str =  您无法将其转换为int; 
int i;
if int .TryParse(str, out i))
{
// 成功转换为int,你可以在变量中找到值i
}
else
{
// 无法将字符串转换为int
}



如果您使用 Convert.ToInt32 int.Parse 获取上述示例,那么您的程序将抛出一个例外。


浏览这些链接。

类型转换和解析之间的区别 [ ^ ]

difference-between-casting-and-using-the-convert-to-method [ ^ ]

这可能会有所帮助。


Hii team ,


I am very much confused between three things as requested
When to cast , when to parse and when to convert ?

For example

(int) obj.value;

int.parse(obj.value);

convert.toint32(obj.value);

please tell me when to use this things , and what is the actual difference between these three

解决方案

Parse when you have a string containing a representation of a value. For example, when you have a user entering data into a TextBox and you need to use it as a number:

int value = int.Parse(myTextBox.Text);


Or preferably, use TryParse instead:

int value;
if (int.TryParse(myTextBox.Text, out value))
   {
   ... use value
   }
else
   {
   ...report input error to user
   }



Convert is the equivalent of Parse for strings, but it doesn't have a TryParse version, and it's slightly slower because it has to decide what type the parameter is: so if you have an object value to convert to an int Convert.ToInt32 will sort out the type and do it.

Casting does not do the same job: it's a syntactic sugar for "I know what this is, just make it so." - it doesn't change the actual object, it just changes the type of it (with minor exceptions, such as casting from a double to an int: that truncates the double and throws away the fractional part). But it won't change an object type unless they are castable: which means casting a derived type to an base type, or if an explicit cast operator exists (such as double -> int and so forth). You can't use a cast to change a string to an int!

So:
1) If you have a string, use Parse, or TryParse (or TryParseExact)
2) If you have an object and you don't necessarily know what type it is, use Convert
3) If you do know what type it is and just need to refer to it in a different way, cast it.


You need to cast if you have, for example, an int that's stored in an object, byte, short ... For example, if you stored an int in a session variable, then you need to cast:

int yourInt = (int) Session["yourInt"];


For the difference between int.Parse and Convert.ToInt32, please see:
http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32[^]
Use one of these methods if you have a string, which is just an integer represented as a string, for example:

string str = "100";
int i = Convert.ToInt32(str); // or int.Parse
// Only do this if you are 100% sure that it's possible to convert the string to an int. If you are not 100% sure, use int.TryParse


If you have user input (or if you are not 100% sure that you can convert a string to an int), then you should use neither casting, neither int.Parse and neither Convert.ToInt32. You need to use int.TryParse[^], because TryParse checks first whether it's possible to parse it to an int:

string str = "You can't convert this to an int";
int i;
if (int.TryParse(str, out i))
{
   // successfully converted to an int, you can find the value in the variable i
}
else
{
   // couldn't convert string to int
}


If you would use Convert.ToInt32 or int.Parse for the above example, then your program would throw an exception.


Go through these links.
difference-between-typecasting-and-parsing[^]
difference-between-casting-and-using-the-convert-to-method[^]
This may help.


这篇关于何时投射,何时解析以及何时转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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