VaR is a dynamic parameter. It dynamically obtains the type of the data to be convenient to define its own type. In theory, the unpacking process is not involved. Object is a data type that involves the data unpacking and packing process, which occupies the memory.
Q: Does var report an error or trigger an exception when the stored data type is not the same?
Verification: VaR is suitable for convenience when the data type is not temporary.
1 arraylist arry = new arraylist (); 2 Arry. add (123); 3 Arry. add (123); 4 Arry. add (123); 5 Arry. add ("AAA"); 6 Arry. add ("AAA"); 7 Arry. add ("AAA"); 8 foreach (VAR item in arry) 9 {10 console. writeline (item); 11} 12 console. writeline ("-----------"); 13 foreach (Object item in arry) 14 {15 console. writeline (item); 16}Foreach
In foreach, the parameter object and VaR are used differently.