The following code:
1 Public class Age2 {3 4 Private intAgenum;5 6 Public intAgenum {7 Get{ 8 return This. Agenum;9 }Ten One Set{ A This. Agenum =value; - } - } the - Public intGetagenum () { - returnAgenum; - } + - Public voidSetagenum (intAge ) { + This. Agenum =Age ; A } at -}
In main, do the following:
1 New Age (); 2 Age.setagenum (234); 3 Console.WriteLine ("age=" + age . Agenum); 4 5 122 ; 6 Console.WriteLine ("age=" + age.getagenum ());
Output:
age=234
age=122
But if you use set get in shorthand, the code is as follows
Public classAge {Private intAgenum; Public intAgenum {Set;Get; } Public intGetagenum () {returnAgenum; } Public voidSetagenum (intAge ) { This. Agenum =Age ; } }
The contents of main are unchanged, and the following results are obtained
Age=0
age=234
If the code order in main is modified as follows
1 122 ; 2 Console.WriteLine ("age=" + Age.getagenum ()); 3 4 age.setagenum (234); 5 Console.WriteLine ("age=" + age. Agenum);
Result is
Age=0
age=122
"Trap for Get set shorthand in 001:c#"