C#: using Linq for a deep copy

Dirty little console app, but it illustrates the point well enough, I think:

LinqDeepCopy01

LinqDeepCopy02

LinqDeepCopy03 LinqDeepCopy04

Snippet

using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApplication3 {
    class Program {
 
        private List<Shoe> _shoesByRef;
        private List<Shoe> _shoesByValue;
 
        public List<Shoe> MyShoes {
            get { return _shoesByRef; }
            set { _shoesByRef = value; }
        }
 
        static void Main(string[] args) {
            var shoes = new List<Shoe> {
                new Shoe { ShoeName = "Clarks", ShoeSize = 12 },
                new Shoe { ShoeName = "Tuf", ShoeSize = 4 }
            };
 
            var p = new Program(shoes);
 
        }
 
        public Program(List<Shoe> shoeSet) {
            // _shoesByRef is a reference to the passed-in shoeSet...
            _shoesByRef = shoeSet;
            // _shoesByValue on the other hand is a deep-copy of _shoesByRef...
            _shoesByValue = new List<Shoe>(_shoesByRef.Select(s => 
                                new Shoe() { ShoeName = s.ShoeName, ShoeSize = s.ShoeSize }));
            // That means we can now set _shoesByRef to null...
            _shoesByRef = null;
            // ... and because _shoesByValue is independent of _shoesByRef, then it still exists:
            foreach (var shoe in _shoesByValue) {
                System.Console.WriteLine("{0};{1}", shoe.ShoeName, shoe.ShoeSize);
            }
        }
    }
 
    internal class Shoe {
 
        private int _shoeSize;
 
        public int ShoeSize {
            get { return _shoeSize; }
            set { _shoeSize = value; }
        }
 
        private string _shoeName;
 
        public string ShoeName {
            get { return _shoeName; }
            set { _shoeName = value; }
        }
    }
}

This mod is to check that the copy went well. Annotation in the code hopefully enough…

LinqDeepCopy05

LinqDeepCopy06

Snippet

using System.Collections.Generic;
using System.Linq;
 
namespace ConsoleApplication3 {
    class Program {
 
        private List<Shoe> _shoesByRef;
        private List<Shoe> _shoesByValue;
 
        public List<Shoe> MyShoes {
            get { return _shoesByRef; }
            set { _shoesByRef = value; }
        }
 
        static void Main(string[] args) {
            var shoes = new List<Shoe> {
                new Shoe { ShoeName = "Clarks", ShoeSize = 12 },
                new Shoe { ShoeName = "Tuf", ShoeSize = 4 }
            };
 
            var p = new Program(shoes);
 
        }
 
        public Program(List<Shoe> shoeSet) {
            // _shoesByRef is a reference to the passed-in shoeSet...
            _shoesByRef = shoeSet;
            // _shoesByValue on the other hand is a deep-copy of _shoesByRef...
            _shoesByValue = new List<Shoe>(_shoesByRef.Select(s => 
                                new Shoe() { ShoeName = s.ShoeName, ShoeSize = s.ShoeSize }));
            // That means we can now set _shoesByRef to null...
            _shoesByRef = null;
            //shoeSet = null;
            // ... and because _shoesByValue is independent of _shoesByRef, then it still exists:
            foreach (var shoe in _shoesByValue) {
                System.Console.WriteLine("{0};{1}", shoe.ShoeName, shoe.ShoeSize);
            }
 
            // Check that the original set has been faithfully copied (expect "True" if it has)...
            var allFound = shoeSet.All(e => _shoesByValue.Any(es => (es.ShoeName == e.ShoeName && es.ShoeSize == e.ShoeSize)));
            System.Console.WriteLine(allFound);
 
            // now set 1 of the byvalue properties to a different value so that the source will not find a match in 1 case:
            // (Now expect false...)
            _shoesByValue[0].ShoeSize = 42;
            foreach (var shoe in _shoesByValue) {
                System.Console.WriteLine("_shoesByValue: {0};{1}", shoe.ShoeName, shoe.ShoeSize);
            }
            foreach (var shoe in shoeSet) {
                System.Console.WriteLine("shoeSet: {0};{1}", shoe.ShoeName, shoe.ShoeSize);
            }
 
            allFound = shoeSet.All(e => _shoesByValue.Any(es => (es.ShoeName == e.ShoeName && es.ShoeSize == e.ShoeSize)));
            System.Console.WriteLine(allFound);
 
        }
    }
 
    internal class Shoe {
 
        private int _shoeSize;
 
        public int ShoeSize {
            get { return _shoeSize; }
            set { _shoeSize = value; }
        }
 
        private string _shoeName;
 
        public string ShoeName {
            get { return _shoeName; }
            set { _shoeName = value; }
        }
    }
}
Advertisements

A little linq


List<string> _allComposers = new List<string>(new[] {"Gaetano","Philidor","Mozart","Bach"});
List<string> _majorComposers = new List<string>(new[] {"Mozart","Bach"});

_allComposers.Dump(“All”);
_majorComposers.Dump(“Major”);

List<string> _minorComposers = new List<string>();
_allComposers.ForEach(x => _minorComposers.Add(x));
_minorComposers.Dump(“Temp”);

_majorComposers.ForEach(x => _minorComposers.Remove(x));
_minorComposers.Dump(“Minor”);
composers01