Guest User

Untitled

a guest
Jan 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         public class Fruits
  11.         {
  12.             public string Name;
  13.             public Fruits(string name)
  14.             {
  15.                 this.Name = name;
  16.             }
  17.         }
  18.  
  19.         public static void TestWeakRef()
  20.         {
  21.             Fruits apple = new Fruits("Apple");
  22.             Fruits orange = new Fruits("Orange");
  23.  
  24.             Fruits fruit1 = apple;
  25.             WeakReference fruit2 = new WeakReference(orange);
  26.  
  27.             Console.WriteLine("(1) Fruit1 = \"{0}\", Fruit2 = \"{1}\"",
  28.                 fruit1.ToString(), fruit2.Target == null ? "" : fruit2.Target.ToString());
  29.  
  30.             apple = null;
  31.             orange = null;
  32.  
  33.             GC.Collect();
  34.  
  35.             Console.WriteLine("(2) Fruit1 = \"{0}\", Fruit2 = \"{1}\"",
  36.                 fruit1 == null ? "" : fruit1.ToString(), fruit2.Target == null ? "" : fruit2.Target.ToString());
  37.         }
  38.         static void Main(string[] args)
  39.         {
  40.             TestWeakRef();
  41.         }
  42.     }
  43. }
Add Comment
Please, Sign In to add comment