public MPTNode Clone() { switch (type) { case NodeType.BranchNode: var n = new MPTNode { type = type, Reference = Reference, Children = new MPTNode[BranchChildCount], }; for (int i = 0; i < BranchChildCount; i++) { n.Children[i] = Children[i].CloneAsChild(); } return(n); case NodeType.ExtensionNode: return(new MPTNode { type = type, Key = (byte[])Key.Clone(), Next = Next.CloneAsChild(), Reference = Reference, }); case NodeType.LeafNode: return(new MPTNode { type = type, Value = (byte[])Value.Clone(), Reference = Reference, }); case NodeType.HashNode: case NodeType.Empty: return(this); default: throw new InvalidOperationException(nameof(Clone)); } }