int.TryParse type conversion c # () method

public static bool TryParse(string s, out Int32 result);

If the conversion is successful it returns true. False otherwise

int.TryParse (string s, out int i) of Parameters: s is the string to be converted, i is the result of the conversion.

  1. Successful execution returns true, the output value of a successful conversion; failed return 0
Been tested and found as follows: 

int I = 0 ; 

. 1 , S is a null 

int .TryParse ( null , OUT I) 
 to false 

2 , S is an empty string 

int .TryParse ( "", OUT I) 
 to false 

. 3 , with the string S spaces. 

int .TryParse ( " . 1 ", OUT I) 
 to true  
int .TryParse ( " . 1 ", OUT I) 
 to true  
int .TryParse ( " . 1 ", OUT I) 
 to true  
int .TryParse ( " . 1  2 ",OUT I) 
 false 

. 4 , non-numeric strings of a definitely false. 

5 , the operation of the database, often encountered the DBNull 

int .TryParse (to System.DBNull, OUT I) 
"to System.DBNull" is a "type", which is not valid in the given context 

int .TryParse (to System.DBNull .Value, OUT I) 
and " int .TryParse ( String , OUT  int )" overloaded method having the best match some invalid parameter 
 int .TryParse (System.DBNull.Value.ToString (), OUT I) 
 to false
Determining whether an input frame is an integer
             int BaseNum = default ( int );
             IF (! Int .TryParse (txtBaseNum.Text, OUT BaseNum)) 
            { 
                MessageBox.Show ( " Please enter an integer. " );
                 Return ; 
            }
@ Syntax: int.TryParse (str, OUT RES) 

int RES = . 1 ;
 int I = . 3 ;
 String str = "" ;
 IF ( int .TryParse (str, OUT RES)) {
     // Because of type String str , so this transition is certainly failed, failed to return to false
     // so it will not come in the inner loop
     // since fails, the value of the res 0 
}
 IF ( int .TryParse (I, OUT res)) {
     // because i is of type int, so the conversion is successful, a successful return true
     // it will enter into the inner loop
     // because of the successful execution, the value of the res i, i.e. 3 
} 
--------- -------
Disclaimer: This article is the original article CSDN bloggers "Dear_BigTiger", and follow CC 4.0 BY- SA copyright agreement, reproduced, please attach the original source link and this statement. 
Original link: HTTPS: // blog.csdn.net/Dear_BigTiger/article/details/81033844

Summary: TryParse can reduce the probability of error for our program and help us complete and accurate business requirements, can also be used to determine the time entered by the user, the amount and quantity is not in line with the rules

Guess you like

Origin www.cnblogs.com/michellexiaoqi/p/11617191.html