C # type conversion of primary articles (1)
Classification of type conversion
Programming friends learned that type conversion, and also clear “implicit conversion”, “explicit conversion”, “packing”, “unpacking” and so on. But how many kinds of type conversion? How it can be divided? What type should be used when converting?
This will be a brief explanation of these issues. Be noted that almost all of the content in this article come from my lectures in the process of accumulation, in this strongly to thank my students, especially for my views and recommendations of the participants to question! In addition, this is not necessarily the actual programming work for you too much help, but you can cast a new understanding.
1, related concepts
Time type conversion occurs (Occasion) - When the value of replication occurs when the type conversion may occur. The so-called “value of copy,” including assignment operators and methods of mass participation. If the assignment in the form of a variable or method parameter types and the actual object types, on the need for type conversion.
Here are two types of conversion of the case occurred.
1 / / Example 1
2int x = 10;
3long y = x; / / type conversion occurs here, because the x and y of type.
4
5 / / Example 2
6void F (long var) … (…)
7
8void G ()
9 … (
10 int i = 10;
11 F (i); / / type conversion occurs here because the arguments i and different type of parameter var.
12)
13
Source type (Source Type) and target type (Destination Type) - type conversion when there was an assignment of variable or method parameter types as the target type, while the actual object type is called a source type.
Such as the above example 1, the variable x of type int is the source type, the type of long and variable y is the target type; Example 2, the variable i of type int is the source types, and methods of parameter F var of the type long is the target type.
2, the classification of type conversion
C #, type conversion, there are two classifications, one based on the different ways to convert classified, can be divided into explicit (Explicit) conversion and implicit (Implicit) conversion of two; the other is based on source type and target type of relationship between the division can be divided into transform (Conversion), projection (Cast), and boxing / unboxing (Boxing / Unboxing).
The following diagram depicts the various types of this transformation, and their occurrences.
2.1 Explicit and implicit conversion conversion
From the intuitive point of view, explicit and implicit conversion is the difference between the above syntax. When type conversion occurs, if explicitly specified in the code the target type is called explicit conversion, or is called implicit conversion.
C # by the form “(objective type)” This kind of grammar structure on the front of the object to be converted to represent an explicit conversion.
The following code describes the C # language for explicit and implicit conversion syntax.
int x = 10;
long y = x; / / implicit conversion
x = (int) long; / / Display conversion
Note that, not between any two types can easily be converted. In addition, whether explicit or implicit conversion conversion may fail.
If explicit conversion fails, an exception is thrown at run time (the exception may be InvalidCastException, it may be InvalidOperationException, OverflowException other specific exceptions); If the implicit conversion fails, it will get an error at compile time that can not be hidden conversion.
Finally, the implicit conversion can also be replaced with an explicit conversion, but conversion was not replaced with implicit conversion. In other words, you can use explicit conversion of the place, with the implicit conversion is also no problem; but requires explicit conversion of the place, must not use the implicit conversion.
The following description from another point of view of different types of conversion.
Recommended links:
I Have A A4 Paper Give You A High Performance [1]
Easy to use Gallery And Cataloging Tools
hal.dll DAMAGED the problems caused by Ubuntu not boot
Political Consultative Conference, said the computer is free to perform their duties need to send
Live Messenger cause 100% CPU UTILIZATION to solve
Directory Multimedia Creation Tools
The basics of INTRUSION detection rules
Foxconn Jumped Staff Re: The Suspect Being Chased Illusion Trap

After read blog topic’s related post now I feel my research is almost completed. happy to see that.Thanks to share this brilliant matter.
Comment by Biochemistry Thesis — October 19, 2010 @ 11:44 am
Very informative and helpful.I was searching for this information but there are very limited resources.Thank you for providing this information
Assignments Writing Help
Comment by UK Assignments — October 21, 2010 @ 4:45 pm