Implicit conversion: Slave TypeATo typeBThe conversion can be performed under all circumstances. The conversion execution rules are very simple and can be performed by the compiler.
Display conversion: Slave TypeATo typeBThe conversion can only be performed in some situations. The conversion rules are compared and assigned values, and some type of processing should be performed.
Implicit conversion rules: Any TypeA, As long as the value range is completely included in the typeBCan be implicitly converted to the typeB
When explicitly requiring the compiler to convert a value from one data type to another, it is to execute display conversion.
Display the conversion syntax structure:Destinationtype(Sourcevar) Is the type of the target variable (the value of the source variable)
IsSourcevarToDestinationtypeType
Note: Display conversion is only feasible in some cases. Data type conversion cannot be performed for a type that has no relationship with each other.
Display conversion is to convert a type with a large value range to a type with a small value range. Therefore, data loss occurs during display conversion, that is, overflow.
We can useCheckedAndUncheckedKeyword to check for overflow. They are called expression overflow check environments.
CheckedKeywordsEnable overflow check for integer arithmetic operations and conversions.
By default, if the value generated by the expression exceeds the range of the target type, the constant expression will cause a compilation error, and the constant expression will be computed at runtime and cause an exception. However, if the overflow check is canceled globally using the compiler option or environment configuration, you can useCheckedKeyword to enable this function.
Example:
This example shows how to use a non-numeric expressionChecked. Overflow is reported during running.
Using system;
Class overflowtest
{
Static short x = 32767; // Max short Value
Static short Y = 32767;
// Using a checked expression
Static int checkedmethod ()
{
Int z = 0;
Try
{
Z = checked (short) (x + y ));
}
Catch (system. overflowexception E)
{
Console. writeline (E. tostring ());
}
Return Z;
}
Static void main ()
{
Console. writeline ("Checked output value is: {0 }",
Checkedmethod ());
}
}
Sample output:
System. overflowexception:Arithmetic Operations cause overflow.
InOverflowtest. checkedmethod ()LocationD: "My Documents" Visual Studio 2005 "proje
CTS "leleapplication1" consoleapplication1 "program. CS:Row number14
Checked output value is: 0
UncheckedKeywordsUsed to cancel overflow checks for integer arithmetic operations and conversions.
In an unchecked context, if the expression generates a value out of the target type range, the result is truncated.
Example:
Unchecked
{
Int val = 2147483647*2;
}
IntValue Range: In-2147483648 ~ 2147483647Integer
Because the above calculationUncheckedSo the result is too large for integers.ValAssigned Value-2. By default, overflow Detection is enabled,
This is usedCheckedWith the same effect.
In the preceding exampleUncheckedWill generate a compilation error because the expression uses constants and the results are known during compilation.UncheckedKeyword also cancels the logarithm
Expression overflow detection to avoidOverflowexception.
UncheckedThe keyword can also be used as an operator, as shown below:
Public int uncheckedadd (int A, int B)
{
Return unchecked (A + B );
}
Example:
This example usesUnchecked, Show how to useUncheckedStatement.
Using system;
Class testclass
{
Const int x = 2147483647; // Max int
Const int y = 2;
Static void main ()
{
Int Z;
Unchecked
{
Z = x * Y;
}
Console. writeline ("unchecked output value: {0}", Z );
}
}
Sample output:
Unchecked output value:-2