Esempio n. 1
0
        private static void TestDeepCopy()
        {
            Console.WriteLine("Cloning works:");
            DeepPaper oxNews = new DeepPaper("Ox News");

            oxNews.Insert(new NewsArticle("Corona stops production", "The beer manufacturer...", "Bill. D. Newsly"));
            DeepPaper fakeNews = oxNews.Clone() as DeepPaper;

            Console.WriteLine(oxNews);
            Console.WriteLine(fakeNews);

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("\nAdding a new article in the original - does not change the copy:");
            oxNews.Insert(new NewsArticle("Man bites dog", "Yesterday afternoon...", "Martha M. Newsworthy"));
            Console.WriteLine(oxNews);
            Console.WriteLine(fakeNews);

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("\nEven changing something in an article in the original - does not change the copy:");
            oxNews.ChangeTitle(0, "New title");
            Console.WriteLine(oxNews);
            Console.WriteLine(fakeNews);

            Console.ResetColor();
        }
Esempio n. 2
0
        public DeepPaper(DeepPaper paper)
        {
            name     = paper.name;
            articles = new List <NewsArticle>();
            foreach (var article in paper.articles)
            {
                // The list holds the references - you can not just
                // copy that reference and add it to the other list,
                // as it would point to the same object
                articles.Add(new NewsArticle(article));
            }

            // Shorter way of doing it:
            // articles = paper.articles.ConvertAll(article => new NewsArticle(article));
            // Or with linq:
            // articles = paper.articles.Select(article => new NewsArticle(article)).ToList();
        }