V. Conversion of data types

Source: Internet
Author: User

using system;using system.collections.generic;using system.linq;using system.text; Namespace _5. Data type conversion {    class program    {         static void main (String[] args)          {            //  Implicit type conversions             {                 ushort destinationVar;                 char sourceVar =  ' A ';                                 //  The following statement implicitly converts the char type to the ushort type.    &nbsP;            destinationvar = sourcevar ;                 Console.WriteLine ("Sourcevar val: {0}",  sourcevar);                 console.writeline ("destinationvar val: {0}",  destinationvar);            }                          //  Explicit type conversions              {                byte  destinationvar;                 short sourcevar = 7;                                 //  The following statement explicitly converts the short type to a byte type.                 //  If the type conversion is not enforced, a compilation error occurs.                 destinationvar  =  (Byte) sourcevar;                 console.writeline ("sourcevar val: {0}",  sourcevar);                 console.writeline ("DestinationVar  val: {0} ",  destinationvar);             }                 & nbOverflow check context for sp;      //  expressions              {                 byte destinationVar;                 short sourceVar = 281;                            The maximum value of the       // byte type is 255, so this statement will overflow after forcing the type conversion. The result is 25                 destinationvar =  (Byte) sourcevar;                 // destinationvar = checked ((byte) sourcevar);                  //  OverflowException exception occurs when overflow checking is performed on this statement above.                 //  Destinationvar = unchecked ((byte) sourcevar);                  //  The above statement means no overflow check and no overflow checking, the results are the same.                  Console.WriteLine ("Sourcevar val: {0}",  sourcevar);                 console.writeline ("destinationvar val: {0}",  destinationvar);            }                          //  convert using the Convert class method             {                 bool boolval = true;                 double doubleVar = 99.999;                 string stringResult;                 string stringVal =  "17";                 int  integerval = 67;                 long longResult;                                  stringresult =  Convert.ToString (boolval)  + convert.tostring (Doublevar);                 console.writeline ("Stringresult: {0}",  stringResult) ;                                 longResult =  Integerval + convert.toint64 (Stringval);                 console.writeline ("Longresult: {0}",  longresult);             }             console.readkey ();         }    } }/** *  data type conversion  * 1.  basic concepts  * a)   Different types of variables use different patterns to represent data. &NBSP;*&NBSP;B)   They may occupy the same storage space, orThe allowable target type has enough storage space to contain all the source data bits, and the result may be different from the expectation.  *  * 2.  data Type Conversion classification  *  can be divided into implicit type conversions and explicit type conversions.  *  *  II, implicit type conversion  * 1.  basic concepts  *  conversions from type A to type B can be done in all cases, it is very simple to execute the conversion rules, You can have the compiler perform the conversion.  *  * 2.  implicit conversion rules  *  any type a can be implicitly converted to type B as long as its range of values is fully contained within the range of the value of type B.  *  * 3.  basic type implicit conversion table  *  type     can be safely converted to  * byte    short,ushort,int,uint,long,ulong,float,double,decimal * sbyte  short,int,long, Float,double,decimal * short  int,long,float,double,decimal * ushort int, uint,long,ulong,float,double,decimal * int    long,float,double,decimal *  uint   long,ulong,float,double,decimal * long   float,double, Decimal * ulong  float,double,decimal * float  double * char    ushort,int,uint, Long,ulong,float,double,decimal *  * bool and string types do not have an implicit conversion.  *  *  three, explicit type conversion  * 1.  basic concepts  *  conversions from type A to type B can only be done in some cases, and conversion rules are more complex. Some kind of processing should be performed.  *  * 2.  conversion rules  *  forcing data to be converted from one type to another type.  *  its syntax is: *  < (destinationtype) sourcevar>; *   Such conversions can only be feasible in some cases. Types that are not related to each other or types that do not have a relationship at all cannot be cast.  *   *  the overflow check context of the expression  * 1.  simply checks the value of the source variable and compares it to the range of values of the target variable.  * 2.  forces the system to pay special attention to transitions during operation.  *   use the checked and unchecked keywords to make an overflow check context for an expression.  *   its use syntax:  *      checked (expression)       < checks if an expression overflows, and throws an OverflowException exception if an overflow condition occurs > *       Unchecked (expression)    < does not check for an overflow condition > *       *   V. Use the Convert class for explicit conversions  * 1.  effective expression of numeric values  *  first an optional symbol (plus or minus), followed by a 0-bit or multiple-digit number, a sentencePoint followed by one or more digits, followed by an optional E or e, *  followed by an optional symbol and one or more digits (before or after the sequence) and spaces.  *  * 2.  Conversion Method  * //  integer type  * convert.tobyte (val)        val conversion to Byte * convert.tosbyte (Val)      val to SByte  * convert.toint16 (Val)      val converted to Short * convert.touint16 (Val)     val conversion to Ushort * convert.toint32 (val)      val to int  * convert.touint32 (Val)     val conversion to Uint * convert.toint64 (Val)       val conversion to Long * convert.touint64 (Val)     val to ulong *   * //  floating-point type  * convert.tosingle (val)     val converted to float *  convert.todouble (Val)     val conversion to Double * convert.todecimal (val)     val converted to decimal *  * //  boolean type &NBSP;*&NBSp Convert.toboolean (val)    val convert to bool *  * //  character type  *  Convert.tochar (val)       val convert to char *  * //  string type   * convert.tostring (Val)     val conversion to string *  *  using the above type conversion method will always carry out overflow checking, The checked and unchecked keywords do not work.  */


This article is from the "MK IT Life" blog, so be sure to keep this source http://vikxiao.blog.51cto.com/9189404/1585741

V. Conversion of data types

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.