PrepAway - Latest Free Exam Questions & Answers

Which code segments should you include in Target 1 and …

DRAG DROP
You are creating a class named Data that includes a dictionary object named _data.
You need to allow the garbage collection process to collect the references of the _data object.
You have the following code:

Which code segments should you include in Target 1 and Target 2 to complete the code? (To answer, drag the
appropriate code segments to the correct locations in the answer area. Each code segment may be used once,
more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.)
Select and Place:

PrepAway - Latest Free Exam Questions & Answers

Answer:

Explanation:
* WeakReference influences the garbage collector. Most objects that are referenced must be kept in memory
until they are unreachable. But with WeakReference, objects that are referenced can be collected.
* Example: C# program that uses WeakReference
using System;
using System.Text;
class Program
{
/// <summary>
/// Points to data that can be garbage collected any time.
/// </summary>
static WeakReference _weak;
static void Main()
{
// Assign the WeakReference.
_weak = new WeakReference(new StringBuilder(“perls”));
http://www.dotnetperls.com/weakreference

4 Comments on “Which code segments should you include in Target 1 and …

  1. amgcgoncalves says:

    The question is very dubious. “You need to allow the garbage collection process to collect the references of the _data object” do they mean the references of the objects stored in the dictionary or the references to the dictionary itself?

    Also, what’s a “new Class(i * 2)”? The type “Class”, where is it defined?




    5



    0
  2. ronzhong says:

    Not quite understand what’s doing for this question:

    using System;
    using System.Collections.Generic;

    namespace _19_WeakReference
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(“Hello World!”);

    var data = new Data(10);
    }
    }

    public class Data
    {
    Dictionary _data;

    public Data(int count)
    {
    _data = new Dictionary();

    for (int i = 0; i < count; ++i)
    {
    _data.Add(i, new WeakReference(i*2,false));
    }
    }

    }
    }




    0



    0

Leave a Reply