Difference between forced conversion, Convert conversion, and Parse conversion in C # (2)

Source: Internet
Author: User

V

Parse, Covert. To, TryParse type conversion

(15:14:26)

Tags:

Miscellaneous
Category: Asp.net

The two methods can convert string to int. What are their differences? When should I use it? Performance.
In fact, Int32.TryParse in 2.0 also achieves the same effect.

Using System;

Using System. Collections. Generic;

Using System. Text;

 

Namespace ConsoleApplication1

{

Class Program

{

Static void Main (string [] args)

{

String myString = "1234 ";

Int myint = 0;

 

Myint = Convert. ToInt32 (myString );

Console. Write (myint + "\ r \ n ");

 

Myint = Int32.Parse (myString );

Console. Write (myint + "\ r \ n ");

 

Int32.TryParse (myString, out myint );

Console. Write (myint + "\ r \ n ");

 

}

}

}

On the surface, it can be seen that the three methods have achieved the same effect!
Let's change the code:

// String myString = "1234 ";
String myString = null;
Int myint = 0;
Myint = Convert. ToInt32 (myString );
Console. Write (myint + "\ r \ n ");
Myint = Int32.Parse (myString );
Console. Write (myint + "\ r \ n ");
Int32.TryParse (myString, out myint );
Console. Write (myint + "\ r \ n ");

Running result:
If Convert. ToInt32 () is null, 0 is returned instead of an exception;
Int32.Parse () should throw an exception;
If Int32.TryParse () does not throw an exception, true or false is returned to indicate whether the resolution is successful. If a parsing error occurs, the caller will get a value of 0.

Conclusion:
There are almost no differences between the three methods!

If you want to pursue perfection, consider the performance differences:
Int32.TryParse () is better than Int32.Parse () than Convert. ToInt32 ().

Personal suggestion: Use Int32.Parse () in. NET1.1; Use Int32.TryParse () in. NET2.0 ().

Why?
Because: Convert. toInt32 will delegate the final parsing work to Int32.Parse, while Int32.Parse and Int32.TryParse respectively proxy the parsing work directly to Number. parseInt32 and Number. tryParseInt32: the former throws an exception when a parsing error occurs, while the latter returns only false.

 

 

Convert. ToInt32 is similar to int. Parse. Actually, Convert. ToInt32 calls int. Parse:

If the Convert. ToInt32 parameter is null, 0 is returned;The Object inherited from the Object is converted to int.
If the int. Parse parameter is null, an exception is thrown.         Convert String to int

If the Convert. ToInt32 parameter is "", an exception is thrown;
If the int. Parse parameter is "", an exception is thrown.

Convert. ToInt32 can be converted to many types;
Int. Parse can only convert numeric strings.
Int. TryParseSimilar to int. Parse, but it does not produce exceptions. If the conversion is successful, true is returned, and if the conversion fails, false is returned. The last parameter is the output value. If the conversion fails, the output value is 0.

Example: string str = "1234"; int32.parse (str); Convert. toint32 (str );

Let's look at the base class of system:
Take int-string type conversion without format conversion as an example. When int32.parse (str) is used, it is actually calling the Parse method in int32 of the construction type in the system class of the mscorlib base class library. This method calls an Overload method (Parse) in this constructor.
Public static int Parse (string s)
{
Return Parse (s, NumberStyles. Integer, null );
}

In the overloaded parse, the read-only function Nunber in the system is called.
Public static int Parse (string s, NumberStyles style, IFormatProvider provider)
{
NumberFormatInfo instance = NumberFormatInfo. GetInstance (provider );
NumberFormatInfo. ValidateParseStyle (style );
Return Number. ParseInt32 (s, style, instance );
}

PasreInt32 in Nubmer performs the conversion.
Well, int analysis is here. View Convert conversion,
When we call the Convert. toint32 method, we can see how the Convert class is executed!

Public static int ToInt32 (string value, IFormatProvider provider)
{
If (value = null)
{
Return 0;
}
Return int. Parse (value, NumberStyles. Integer, NumberFormatInfo. GetInstance (provider ));
}

You will find that he directly finds the Parse method of the int class. This method directly executes the overload method of parse in int32. It can be seen that they eventually need to fall into the Number of read-only classes for type conversion. The performance gap is generated before the Number is reached! In parse, he will find himself directly, and Convert will Convert string to what type will eventually fall on this type of parse, so the conversion of string type is still more efficient.

 

Convert. ToInt32, int. Parse (Int32.Parse), int. TryParse, and (int) can all be interpreted as converting data types to int. What are the differences between them?

Convert. ToInt32 is similar to int. Parse. Actually, Convert. ToInt32 calls int. Parse:

  • If the Convert. ToInt32 parameter is null, 0 is returned;
  • If the int. Parse parameter is null, an exception is thrown.
  • If the Convert. ToInt32 parameter is "", an exception is thrown;
  • If the int. Parse parameter is "", an exception is thrown.
  • Convert. ToInt32 can be converted to many types;
  • Int. Parse can only convert numeric strings.

Int. TryParse is similar to int. Parse, but it does not produce exceptions. If the conversion is successful, true is returned. If the conversion fails, false is returned. The last parameter is the output value. If the conversion fails, the output value is 0.

Int m;
If (int. TryParse ("2"), out m)
{
...
}
Returns true, runs in {}, and assigns m a value of 2;

If (int. TryParse ("ddd"), out m)
{
...
}
Returns false. if {} is not run, m is assigned 0;

(Int) is cast conversion. It can only convert other numeric types to int type. It cannot convert strings. For example, the following example will fail:

String v = "1 ";
Int n = (int) v;

Int I =-1;
Int. TryParse (null, out I );

After the execution is complete, I is equal to 0 rather than-1. Remember.

1 (int) is a type conversion. When we convert from int type to long, float, double, decimal type, we can use implicit conversion, however, Explicit conversions are required from the long type to the int type. Otherwise, a compilation error occurs.

2 int. Parse () is a type of capacity conversion. It indicates converting the numeric content string to the int type. If the string is null, an ArgumentNullException exception is thrown; If the string content is not a number, a FormatException is thrown; OverflowException is thrown if the number indicated by the string content exceeds the range indicated by the int type;

3 int. TryParse is similar to int. Parse, but it does not produce exceptions. If the conversion is successful, true is returned. If the conversion fails, false is returned. The last parameter is the output value. If the conversion fails, the output value is 0.

4 Convert. ToInt32 () is a type-tolerant conversion, but it is not limited to converting a string to the int type, it can also be another type of parameter; comparison: If the Convert. ToInt32 parameter is null, 0 is returned. If the int. Parse parameter is null, an exception is thrown.  If the Convert. ToInt32 parameter is "", an exception is thrown. If the int. Parse parameter is "", an exception is thrown. Convert. ToInt32 can be converted to many types; int. Parse can only be converted to numeric strings.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.