First, type conversion
The CLR allows the object to be converted to its original type or any of its base types. For example, in C #, you can directly convert an object to any class type; if you want to convert the object to any derived type, C # requires explicit conversion, because this conversion may fail, so ask the programmer The description is indeed to perform this conversion of this parent class to subclass, not because the input is incorrect, if this display is not performed in the statement, the compile period is incorrect; of course, if this converted object itself It is indeed a parent class, then it is impossible to convert to subclasses, even if the displayed conversion is made in the statement, it is compiled, but running period errors occur during operation.
"Convert objects to its original type" means that an object may be a SON type when it is created, but it is converted to the Father type, that is, it is just wrapped in a layer of Father's coat, but actually it Or SON type, then convert this object to the SON type, this object is converted to its original type SON, which is of course successful.
In addition: Talking in the first section of Chapter 5: C # compilers are familiar with the primitive type and apply their own rules when compiling code, so some different types, such as INT32 and INT64 can also transform each other. The type conversion rule in this regard is: If the data loss is not caused during the conversion, if the INT64 type is converted to the INT32 type, implicit conversion can be made; if the conversion process is likely to cause data loss, such as int32 type conversion bit int64 Type, Single Type Conversion Bit INT16 type, requires display conversion.
Example 1:
Father f = new father ();
SON S = New Son ();
Father FWRAPS = S; // The object of the subclass can be directly converted to the parent class, as if the subclass wraps a layer of parent class.
SON SS = (SON) FWRAPS; / / The object of the parent class is converted into a subclass, and the conversion must be displayed because this object is actually an object of a subclass, so the conversion can be successful.
Example 2:
Father f = new father ();
SON S = (SON) f; // Try a parent object to convert to a sub-class object, although the display conversion can be compiled, but an error will occur during operation.
Second, IS and AS operators
You can use the IS operator to determine if an object is compatible with a type.
You can simplify the transformation statement using the AS operator and improve efficiency.
Example 1:
SON S = New Son ();
Father fwraps = s;
If (fwraps is son) // determines if an object is compatible with type SON, and the result is compatible.
{
Messagebox.show ("OK");
}
Example 2:
SON S = o as Son; // Try the object O to convert to the SON type, if success, S is not NULL.
IF (s! = null)
{
}
Third, named space and assembly
The namespace allows us to organize the relevant type logic in a piece, but when there is a type of the same name in a different namespace, the type of type of type of the same name must be accessed when the program is quadably referenced. The fully qualified name is used, otherwise it will generate erliness.
Namespaces and assemblies are not necessarily related, and each type in a namespace can be distributed in different program sets; a program set can contain multiple namespace types. When the types in a namespace are distributed in different program sets, if you need to use the classes in all namespaces, you need to load each assembly file into the reference list (item reference list) in the project. Listed in the referenced assembly, not the referenced namespace, so the name of different assembly of the same namespace is displayed, and then there is no separate use of this namespace in the project. The various types don't need to be a particular type that is stored in which program is concentrated. If each assembly defines the same type of namespace, that is, repeatedly defines a type, if in the project This type is used, then a warning will be made when compiling, and inform the compiler to select which type of sets in which program set is compiled. Example 1 (a program set contains multiple namespace types):
//Winui.cs
Using system;
Namespace Winui.Query
{
}
Namespace Winui.Report
{
}
// Put the client interface type organization in Winui.Query namespace, in WinUI.Report, these two namespaces are exist in the assembly Winui.dll in Winui.Query Namespace.