Data type conversion in C #

Source: Internet
Author: User
Tags float double
Type conversion 
Display Conversion
Implicit conversion
Custom Conversion

(1) implicit conversion: Generally, the conversion from low type to high type ensures that the value does not change.
Implicit numeric conversion:
From sbyte to short, Int, long, float, double or decimal.
From byte to short, ushort, Int, uint, long, ulong, float double or decimal.
From short to int, long, float, double, or decimal.
From ushort to int, uint, long, ulong, float, double, or decimal.
From int to long, float, double, or decimal.
From uint to long, ulong, float, double, or decimal.
From long to float, double, or decimal.
From ulong to float, double, or decimal.
From Char to ushort, Int, uint, long, ulong, float, double, or decimal.
From float to double.
There is no implicit conversion to the char type, so the values of other integer types are not automatically converted to the char type.
The floating point type cannot be implicitly converted to the decimal type.

Implicit Enumeration Conversion
Implicit Enumeration conversion allows you to convert a decimal integer 0 to any enumeration type.

Implicit conversion
Conversion from a derived class to a base class
Implicit conversions refer to conversions between a type of reference types. Such conversions can always be successful, so no check is required at runtime.

Packing and conversion
Packing conversion allows implicit conversion of value type to reference type.

(2) display conversion: Also known as forced type conversion. Data correctness cannot be guaranteed.
(Type) (expression)

(3) User-Defined Conversion
All user-defined conversions are static and use the static keyword.
User-Defined conversion display and implicit display, they useImplicit(Implicit conversion) orExplicit(Display conversion) keyword declaration.
Static access rhetorical device conversion rhetorical device operator Conversion Type (parameter)

Example:

Using system;
Struct number
{
Private int value;
Public number (INT value)
{
This. value = value;
}
// User-Defined implicit conversions from integer to number type
Static public implicit operator number (INT value)
{
Return new number (value );
}
// User-Defined display conversion from number type to integer type
Static public explicit operator int (number N)
{
Return N. value;
}
// User-Defined implicit conversion from number type to string type
Static public implicit operator string (number N)
{
Return N. tostring ();
}
}

Class Test
{
Static public void Main ()
{
Number N;
N = 10;
Console. writeline (INT) N );
// Implicit conversion to string
Console. writeline (N );
}
}

Use the system. Convert class

Converts a basic data type to another basic data type.

Use the parse Method

Most pre-defined value types Use this static method to convert the corresponding text to the corresponding value type.

Packing and unboxing

Binning and unboxing enable conversion between value types and object types.

Packing conversion allows implicit conversion of "Value Type" to "reference type ". To bind a value type, you can assign an object instance and copy the value of the value type to the instance.

Example:

In this example, the integer variable I is converted to the object o through packing. In this example, the object retains the original copy of the content, that is, 123.

Using system;
Class testboxing
{
Public static void Main ()
{
Int I = 123;
Object o = I; // implicit packing
I = 456; // change the value of variable I
Console. writeline ("the value-type value = {0}", I); // 456
Console. writeline ("the object-type value = {0}", O); // 123 is the copy value of I
}
}
Unboxing conversion: unboxing conversion allows explicit conversion of the reference type to the value type.

The unboxing operation includes the following two steps: first, check whether the instance of this object is a box-mounted value of a given value type, and then copy the value from the instance.
Example:

The following example illustrates how invalidcastexception is caused by incorrect unboxing. By using try and catch, an error message is displayed when an error occurs.
Using system;
Public class unboxingtest
{
Public static void Main ()
{
Int inti= 123;
Object o = Inti; // boxed
Try
{
Int intj = (short) O; // unboxing is invalid. Short is not the value type of the box. Check whether the instance of this object is a value of the specified value type.
Console. writeline ("unboxing OK .");
}
Catch (invalidcastexception E)
{
Console. writeline ("{0} error: Incorrect unboxing.", e );
}
}
}

Other conversion Operators
As

The as operator is used to perform explicit type conversion of the reference type. If the type to be converted is compatible with the specified type, the conversion succeeds. If the type is incompatible, null is returned.
Expression as type
The as operator is similar to type conversion. The difference is that when the conversion fails, the as operator returns NULL instead of causing an exception.
Example:
Object O1 = "somestring ";
Object O2 = 5;
String S1 = O1 as string; // compatible with S1 = "somestring"
String S2 = O2 as string; // S2 = NULL

Is

The is operator checks whether the object type is compatible with the given type (the object is of this type or is derived from this type ).
Expression is type
Example:
Int I = 10;
If (I is object) // true
{}

Sizeof

The sizeof operator is used to obtain the size (in bytes) of the value type ).
Sizeof (type)
The sizeof operator applies only to the value type, not the reference type.
The sizeof operator can only be used in the unsafe mode.
Example:
Unsafe
{
Console. writeline ("{0}", sizeof (INT ));
}

Original article: http://www.cnblogs.com/qingtianyzl/articles/581566.html

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.