C # (int), Conver.toint32 (), Int. The differences and linkages between the three types of Parse ()--c# Basics

Source: Internet
Author: User

I have just learned C # programming language, summed up a little bit of knowledge, want to share to everyone. After all, just learning this language, not learning deeply, where if there are errors, please help to point out ha, thank you! 1, (int) can be used for conversions of other numeric types, such as single-precision, double-precision, to integer int, and cannot be used to convert a string type, for example:It is not possible to convert a string here (int), the system will error, the program cannot run.   
using System.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {string mystring= "520";            Int Myint;            Myint = (int) myString; Console.WriteLine (Myint);        }    }}
The following example is the conversion of a single precision type float, which is possible  
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            intx; floaty=1.2f; X= (int) y;Console.WriteLine (x);        Console.readkey (); }    }}
use (int): Double or float is converted to type int, the decimal part is not rounded, and the result is rounded
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            //double converts to int, using Convert.ToInt32 ()            DoubleMyint=1.6; stringMyString ="123.45"; //string mystring= "ABCD"; //string myString = "12345";            intIntresult; stringStringresult; Intresult= (int) MyInt;            Console.WriteLine (Intresult);        Console.read (); }    }}

2, Convert.ToInt32 () can convert the string type, but the contents of the string must be "123456", that is, the inside is an integer numeric type. Cannot be "123.456" (which can be thought of as decimals) or "ABCdef ". AlsoConvertible value types, such as double and float. The string is "123.45" and the system will error:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            intmyInt; stringMyString ="123.45"; //string mystring= "ABCD"; //string myString = "12345";            intIntresult; stringStringresult; Intresult=Convert.ToInt32 (myString);            Console.WriteLine (Intresult);        Console.read (); }    }}
use Convert.ToInt32 ():double or float is converted to int, the fractional part can be rounded.   The rule is: integer digits if: 0-4: Drop the decimal digit directly. If the value is 5.6, the rule is: if the integer digit is 5, to return the adjacent even () result is 6 the whole digit is 6-9 Yes, the rounding is to be rounded.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            //double converts to int, using Convert.ToInt32 ()            DoubleMyint=1.6; stringMyString ="123.45"; //string mystring= "ABCD"; //string myString = "12345";            intIntresult; stringStringresult; Intresult=Convert.ToInt32 (MYINT);            Console.WriteLine (Intresult);        Console.read (); }    }}
Output is 2
3, Int. Parse () can only convert string types, cannot be other, such as double to int these numeric types are not allowed. In addition, when you convert a string, andConvert.ToInt32 () with,the contents of the string must be "123456", namely: The inside is an integer numeric type. Cannot be "123.456" (which can be thought of as decimals) or "abcdef".
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            intmyInt; //string myString = "123.45"; //string mystring= "ABCD";           stringMyString ="12345"; intIntresult; stringStringresult; Intresult=int.            Parse (myString);            Console.WriteLine (Intresult);        Console.read (); }    }}
Double-->int, no .
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            //double converts to int, using Convert.ToInt32 ()            DoubleMyint=1.6; stringMyString ="123.45"; //string mystring= "ABCD"; //string myString = "12345";            intIntresult; stringStringresult; Intresult=int.            Parse (MYINT);            Console.WriteLine (Intresult);        Console.read (); }    }}
4, Conver.toint32 () can convert String=null to 0, and (int), int. Parse () cannot convert an empty string, as an instance:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {String myString=NULL;            String resultstring; intmyInt; //myInt = (int) myString;//cannot convert nullMyInt = Convert.ToInt32 (myString);//convert NULL to 0//myInt = Int. Parse (myString);//cannot convert nullConsole.WriteLine (MYINT);        Console.read (); }    }}
5, the int type is converted to a string type, using the coercion type conversion character,Use Convert.ToString () to convert
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            //forced parenthesis Conversions can only convert values of type            intMyInt =3; stringStringresult; Stringresult=(string) MyInt;        Console.read (); }    }}
 
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceAn expression {classProgram {Static voidMain (string[] args) {            //forced parenthesis Conversions can only convert values of type            intMyInt =333; stringStringresult; //Stringresult = convert.tostring (myInt);Stringresult =myint.tostring ();            Console.WriteLine (Stringresult);        Console.read (); }    }}
article is a personal original, please respect the original, reproduced please indicate the source! The article will inevitably have errors, but also hope that you more haihan, in addition to the technical experts want to point out a lot of shortcomings, brother to improve!

C # (int), Conver.toint32 (), Int. The differences and linkages between the three types of Parse ()--c# Basics

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.