Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to convert string data into int data in C#?
How to convert string data into int data in C#?
Either int. Parsing method or int. The TryParse method can convert string data into int data.

Example 1 uses int. Parse method to realize the transformation, which usually needs to catch and handle the exceptions that appear during the transformation.

String? s? =? ……;

int? The result? =? 0;

attempt

{

The result? =? The analysis inside;

Console. WriteLine ("The converted integer value is: {0}",? Results);

}

catch

{

Console. WriteLine ("Cannot complete conversion!" );

} Example 2 uses int. The TryParse method is used to realize the conversion, and there is no need to handle the exceptions that occur during the conversion.

String? s? =? ……;

int? Results;

if(int。 Trypsin (s, out? Results))

{

Console. WriteLine ("The converted integer value is: {0}",? Results);

}

other

{

Console. WriteLine ("Cannot complete conversion!" );

} it is recommended to use int. TryParse method!