示例#1
0
        private static void TestShallowCopy()
        {
            Console.WriteLine("Cloning works:");
            ShallowPaper oxNews = new ShallowPaper("Ox News");

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

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

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

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\nEven changing something in an article in the original - changes both the original and the copy:");
            oxNews.ChangeTitle(0, "New title");
            Console.WriteLine(oxNews);
            Console.WriteLine(fakeNews);

            Console.ResetColor();
        }
示例#2
0
 public ShallowPaper(ShallowPaper paper)
 {
     name = paper.name;
     // This is a shallow copy. Copies only the reference
     // which ends up refering to the same list in all copies
     // which is equivalent to MemberwiseClone():
     articles = paper.articles;
 }