C # graphic tutorials study notes - Conversion

First, what is the conversion
Conversion (Conversion) means accepts values of a type and use it as the equivalent value of another type of process. The converted value source and value should be the same, but the type of the target type.

 

Second, explicit and implicit conversion conversion (cast)
1. implicit conversion
(1) will automatically do the conversion language.
(2) without loss of data or precision, are generally converted to a short length type type.
(2) less the number of bits from the source type to a target type more bits, certain bits in the need to use extra fill 0 or 1.
When the smaller unsigned type is converted to a larger unsigned type, type highest goal of all the extra padding 0, this is called zero-extended (zero extension); for a signed type of conversion, the additional It is filled with the high expression of the symbol bit source, which is called sign extension (sign extension). This will maintain the correct sign of the value and size of the converted.
2. explicit conversion (cast)
if necessary to convert a short length type type, may not provide the type of target source value without loss of data, then must use explicit conversion (cast) expression.
grammar:

3. Overflow detection context
if the code fragments are referred to as overflow detection checked context. The default context is not checked overflow detection.
Explicit conversion data may be lost and can not represent the same type in the target source value. For integer types, C # provides us with the ability to select whether to type conversion during runtime detection result overflow. This will be achieved by operators checked and checked sentence.
If we specify an expression or piece of code is checked, CLR will throw an OverflowException conversion produces abnormal overflow. If you do not specify is checked, the conversion will continue regardless of whether the overflow.
(. 1) checked and unchecked operators
checked and unchecked for overflow detection operators context control expression. (Expression method can not be a)
Example:

(2) checked and unchecked statements
checked and unchecked for controlling all statements converting a code block.
Example:

 

三、引用转换
引用类型对象由引用和数据两部分组成,引用转换接受源引用并返回一个指向堆中同一位置的引用,但是把引用“标记”为其他类型。
1. 隐式引用转换
(1)所有引用类型都可以被隐式转换为object类型。
(2)任何类型都可以隐式转换到它继承的接口。
(3)类可以隐式转换到:它继承链中的任何类、它实现的任何接口。
2. 显式引用转换
显式引用转换是从一个普通类型到一个更精确类型的引用转换。
显式引用转换包括:
(1)从object到任何引用类型的转换。
(2)从父类到子类的转换。
3. 有效显式引用转换
在运行时能成功进行(不抛出InvalidCastException异常)的显示转换有3种情况。
(1)显式转换是没必要的,即语言已经为我们进行了隐式转换。例如,从衍生类到基类的转换总是隐式转换。
(2)源引用是null。
示例:

(3)由源引用指向的实际数据可以被安全地进行隐式转换。

 

四、装箱转换和拆箱转换
1. 装箱转换
装箱是一种隐式转换,它接受值类型的值,根据这个值在堆上创建一个完整的引用类型对象并返回对象引用。
装箱是创建副本,在装箱后,该值有两份副本–原始值类型和引用类型副本,每个都可以独立操作。
示例:

图示:

2. 拆箱转换
拆箱(unboxing)是把装箱后的对象转换回值类型的过程。
(1)拆箱是显式转换。
(2)系统在拆箱时执行如下步骤:
它检测到要拆箱的对象实际是ValueTypeT的装箱值。
它把对象的值复制到变量。
示例:

 

五、用户自定义转换
1. 用户自定义转换语法

2. 用户自定义转换的约束
(1)只可以为类和结构定义用户自定义转换。
(2)不能重定义标准隐式转换或显式转换。
(3)对于源类型S和目标类型T,如下命题为真:
S和T必须是不同类型;
S和T不能通过继承关联;
S和T都不能是接口类型或object类型;
转换运算符必须是S或T的成员。
3. 用户自定义转换示例
示例:

使用implicit运算符定义转换,使用的是隐式转换。如果使用explicit运算符来定义转换,则需要使用强制转换表达式来进行转换。
示例:

4. 多步用户自定义转换
用户自定义转换在完成转换中最多可以有3个步骤。
示例:

图示:

 

六、is运算符和as运算符
1. is运算符
检查转换是否会成功完成。
语法:

只可用于引用转换、装箱、拆箱,不能用于用户自定义转换。
2. as运算符
用于强制类型转换,如果转换失败,返回null而不是抛出异常。
语法:

只能用于引用转换和装箱转换,不能用于用户自定义转换或到值类型的转换。

Guess you like

Origin www.cnblogs.com/wujuntian/p/10990376.html