private static SpriteSheet[] MergeSpriteSheets(SpriteSheet[] originalSheets, SpriteSheet[] newSheets) { Dictionary<string, List<string>> prefixDirectLookup = new Dictionary<string, List<string>>(); List<string> order = new List<string>(); originalSheets = originalSheets ?? new SpriteSheet[0]; newSheets = newSheets ?? new SpriteSheet[0]; foreach (SpriteSheet sheet in originalSheets.Concat<SpriteSheet>(newSheets)) { if (sheet.Id == null) { throw new InvalidOperationException("Sprite sheet is missing an ID."); } if (!prefixDirectLookup.ContainsKey(sheet.Id)) { prefixDirectLookup.Add(sheet.Id, new List<string>()); order.Add(sheet.Id); } prefixDirectLookup[sheet.Id].AddRange(sheet.Prefixes); } return order .Select<string, SpriteSheet>( id => new SpriteSheet() { Id = id, Prefixes = prefixDirectLookup[id].ToArray() }) .ToArray(); }