Exemple #1
0
        private static void TestLongWeakReference()
        {
            ReferenceObject strongReference = new ReferenceObject()
            {
                Id     = 1
                , Name = "Reference Object 1"
            };
            WeakReference <ReferenceObject> weakReference = new WeakReference <ReferenceObject>(strongReference);

            weakReference.TryGetTarget(out ReferenceObject referenceObject1);

            Console.WriteLine($"Reference Object 1: {referenceObject1.Name}");

            strongReference  = null;
            referenceObject1 = null;

            weakReference.TryGetTarget(out ReferenceObject referenceObject2);

            Console.WriteLine($"Reference Object 2: {referenceObject2.Name}");

            referenceObject2 = null;

            GC.Collect();

            weakReference.TryGetTarget(out ReferenceObject referenceObject3);

            Console.WriteLine($"Reference Object 3a: {referenceObject3.Name}");

            weakReference = null;
            GC.Collect();

            Console.WriteLine($"Reference Object 3b: {referenceObject3.Name}");
        }
Exemple #2
0
        private static void TestStrongReferences()
        {
            var o1 = new ReferenceObject()
            {
                Id = 1, Name = "Object 1"
            };
            var o2 = new ReferenceObject()
            {
                Id = 2, Name = "Object 2"
            };
            var o3 = new ReferenceObject()
            {
                Id = 3, Name = "Object 3"
            };

            StrongReferences.Add(o1);
            StrongReferences.Add(o2);
            StrongReferences.Add(o3);
        }
Exemple #3
0
        private static void TestWeakReferences()
        {
            var o1 = new ReferenceObject()
            {
                Id = 1, Name = "Object 4"
            };
            var o2 = new ReferenceObject()
            {
                Id = 2, Name = "Object 5"
            };
            var o3 = new ReferenceObject()
            {
                Id = 3, Name = "Object 6"
            };

            WeakReferences.Add(o1);
            WeakReferences.Add(o2);
            WeakReferences.Add(o3);

            o1 = null;
            o2 = null;
            o3 = null;
        }
 public void Add(ReferenceObject o)
 {
     Objects.Add(o);
 }
Exemple #5
0
 public void Add(ReferenceObject o)
 {
     Objects.Add(new WeakReference <ReferenceObject>(o));
 }