Also, why would anyone need to use new operator with value types (unless
looping over a collection and adding items to it by using the same
variable
but creating a new object each time)



Now how did I miss that? It is ansolutly important to remember that value
types are always transmited by copy, not reference (except when ref or out
are used).

This is what happnes in your loop  and before it:
1) Method entered, from stack space, a portion is reserved to the
'valTypeInstance' var
2)Loop entered
   valTypeInstance = new ValType(); //valTypeInstance is zeroed;
   valTypeInstance.prop1 = <some value>; // a value is copied into
valTypeInstance member prop1
   valTypeInstance.prop2 = <some value>; //a value is copied into
valTypeInstance member prop2
   coll.Add(valTypeInstance);// a *copy *of valTypeInstance is passed to
the method !!!!
3) Loop reentered
   valTypeInstance = new ValType(); // the same valTypeInstance is zeroed;
   .... etc
4) Method exit, Stack pointer decremented, no dealocations are made wich
means phisicaly the value of valTypeInstance still exists in its's former
location on the stack containing the last value it had, but this location is
going to be overriden by other value type vars on next method enter.

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to