1.6 Type system unification)
C # A unique type-a unified system type (I have a headache for this death. Please tell me who has a better name ). In short, all other types, including values and references, can be treated as a unified system type. Conceptually, all types are derived from it. In this way, attributes and methods of the unified system type can be used for other types. Including some "simple" types, such as: int. Let's give an example:
*/
Using System;
Class Test
{
Static void Main (){
Console. WriteLine (3. ToString ());
}
}
/*
"3. ToString ()" calls the "ToString ()" method of the object. I believe that anyone who has learned c/c ++ knows how troublesome it is to output a number. Now it is easy. Another one :*/
Class Test
{
Static void Main (){
Int I = 123;
Object o = I; // boxing
Int j = (int) o; // unboxing
}
}
/*
In this example of a hat-trick, it is converted from "int" to "object" and then back. In this way, a bridge is established between values and references.
How can this be used. An impromptu example is like min. In c/c ++ :*/
// C/c ++ code
Void min (int I, int j)
{
Return (I <j )? I: j );
}
/* What if the comparison is not int, or maybe int, float, or double? You can do this :*/
Template
T min (T I, T j)
{
Return (I <j )? I: j)
}
/* Use c :*/
Void swap (object a, object B)
{
Return (I <j )? I: j );
}
/*
I think we can see that if the second example is to compare an int and a float, some conversions are required, and the third example can compare all the variables! This flexibility is too great. Therefore, I personally think that everyone must be careful when using it!
It will never report an error when comparing an int with a class. Haha, I found that my translation is always getting farther and farther, and I can't hold the original text. You have been tampered with a lot. Please forgive me!