Question here: Question: questions about C # continuous assignment
In msdn, The = Operator is described as follows:
The value assignment operator (=) stores the value of the right operand in the storage location, attribute, or indexer indicated by the left operand and returns the value as a result. The type of the operand must be the same (that is, the right operand must be implicitly converted to the type of the left operand ). First, let's look at int x, y, z;
X = y = z = 1; 1: z = 1, assign 1 to z, then return 1, 2: y = (z = 1), assign 1 to y, returns 1, 3: x = (y = (z = 1), and assigns result 1 of expression 2 to x. Next, let's look at the first question: const int x = 1;
Short y;
Object z;
Z = y = x;
The first thing to note is that x is const, because const is a compilation constant, So Z = y = x; during compilation, it will become z = y = 1. 1: y = 1. Because y is short, 1 is converted to short, so the return value is 1 (short); 2: returns the result of y = 1, that is, 1 (short) is assigned to z, so z is the object after 1 (short) is boxed. GetType returns System. int16. it is worth mentioning that if you remove the const in the preceding const int x = 1, the Code is as follows: int x = 1;
Short y;
Object z;
Z = y = x;
Because x is Int32 and y is Int16, Int32 cannot be implicitly converted to Int16. therefore, this Code cannot be compiled:
Next, consider the second question: class C
{
Private string x;
Public string X
{
Get {return x ?? "";}
Set {x = value ;}
}
} Static void Main ()
{
C c = new C ();
Object z;
Z = c. X = null;
// What are output in the following two statements?
System. Console. WriteLine (z = null );
System. Console. WriteLine (c. X = null );
}
The key is analysis: z = c. X = null; 1: c. X = null; call the setX method of c, set x = null, and return null as the value. 2: z = (c. X = null); Because c. X = null, return null, so assign null to z. z is null at this time; 3: Console. writeLine (z = null), returns True; 4: Console. writeLine (c. X = null), call the getX method of c, the method returns "", so c. returns False if X = null. Are you all right?