Raised directly from C # 3.0 in a nutshell :
When a node or attribute is added to an element (via a functional construct or the Add method), the node property or Parent attribute is set to this element. A node can have only one parent: if you add an already born node to the second parent, the node will automatically be deeply cloned. In the following example, each client has a separate copy of the address:
var address = new XElement ("address", new XElement ("street", "Lawley St"), new XElement ("town", "North Beach") ); var customer1 = new XElement ("customer1", address); var customer2 = new XElement ("customer2", address); customer1.Element ("address").Element ("street").Value = "Another St"; Console.WriteLine ( customer2.Element ("address").Element ("street").Value);
This automatic duplication saves an instance of the X-DOM object without any side effects - another sign of functional programming.
Wonko Oct 17 '08 at 2:00 p.m. 2008-10-17 14:00
source share