Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- public class Fruits
- {
- public string Name;
- public Fruits(string name)
- {
- this.Name = name;
- }
- }
- public static void TestWeakRef()
- {
- Fruits apple = new Fruits("Apple");
- Fruits orange = new Fruits("Orange");
- Fruits fruit1 = apple;
- WeakReference fruit2 = new WeakReference(orange);
- Console.WriteLine("(1) Fruit1 = \"{0}\", Fruit2 = \"{1}\"",
- fruit1.ToString(), fruit2.Target == null ? "" : fruit2.Target.ToString());
- apple = null;
- orange = null;
- GC.Collect();
- Console.WriteLine("(2) Fruit1 = \"{0}\", Fruit2 = \"{1}\"",
- fruit1 == null ? "" : fruit1.ToString(), fruit2.Target == null ? "" : fruit2.Target.ToString());
- }
- static void Main(string[] args)
- {
- TestWeakRef();
- }
- }
- }
Add Comment
Please, Sign In to add comment