C # data type conversion

Source: Internet
Author: User

C # is often used when writing BS code these days, and a simple summary of the C # data type is provided. This article describes the implicit conversion of C #, display conversion, parse, Tostring, and Convert conversion.

Implicit conversion
When converting a simple value type, if the conversion is performed by Byte, short, int, long, float, and double from left to right (from short to long, can be directly converted (implicit conversion) without any instructions. Simple code example:
[Csharp]
Static void Main (string [] args)
{
Int a = 10;
Long B =;
Console. Write ("B value:" + B );
Console. ReadKey ();

}

Display Conversion
If you convert a value type from a long byte to a short byte directly, the compiler will prompt "the type * cannot be converted to a type * and a display conversion exists ", in this case, a forced conversion (display conversion) is required ). Simple code example:
[Csharp]
Static void Main (string [] args)
{
Long a = 10;
Int B = (int);
Console. Write ("B value:" + B );
Console. ReadKey ();
}

Each data type has its own range. For example, the byte type ranges from 0 ~ 255, int type range is 0 ~ 65535. What should I do if int type is converted to byte type beyond its own range? Take a look at the sample code:
[Csharp]
Staticvoid Main (string [] args)
{
Int a = 256;
Byte B = (byte);
Console. Write ("B value:" + B );
Console. ReadKey ();
}

The running result of this code is "the value of B is 0". If the value of a is changed to 257, the value of B is 1. How does the result come from? The compiler will convert 256 to the corresponding binary, that is, 100000000. When it is converted to the byte type (8-bit binary number ), the 8-bit part is truncated, so the result is 0.

ToString () Conversion
When you convert a value type to a string type, you can directly call the toString () method to convert the value type. In addition, toString can also convert the result to the corresponding hexadecimal form, simple code example:
[Csharp]
<Span style = "font-size: 18px;"> staticvoid Main (string [] args)
{
// Convert to the corresponding string type
Int a = 256;
String B = a. ToString ();
Console. Write ("B value:" + B );
Console. ReadKey ();
}
 
Static void Main (string [] args)
{
// Convert to hexadecimal format (X can be changed to lowercase, representing the case of a-f)
Int a = 256;
String B = a. ToString ("X ");
// The output result is "B's value is 100"
Console. Write ("B value:" + B );
Console. ReadKey ();
} </Span>

Parse Method
For int, long, and float types, the parse method can be used to convert strings to corresponding data types. Simple code example:
[Csharp]
<Span style = "font-size: 18px;"> staticvoid Main (string [] args)
{
Int a = 256;
String B = "256 ";
If (int. Parse (B) =)
{
Console. Write ("the values of a and B are equal! ");
Console. ReadKey ();
}
} </Span>

Convert class
Convert has many methods to Convert data types. It converts objects inherited from the Object type to the specified type, that is, Convert. toInt32 () (and conversion of other types ). Another method is Convert. ChangeType (Object, Type). This method is advantageous when it involves generics.

 

 

 


 

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.