Programming in C# — Question 163
You are developing a Windows Forms (WinForms) application. The application displays a TreeView that has 1,000 nodes.
You need to ensure that if a user expands a node, and then collapses the TreeView, the node object is kept in memory unless the Garbage Collector requires additional memory.
Which object should you use to store the node?
Answer options
- A. GC
- B. Handle
- C. Cache
- D. WeakReference
Correct answer: D
Explanation
The correct choice is D, WeakReference, as it allows the node to be referenced without preventing it from being collected by the Garbage Collector when memory is needed. Option A, GC, refers to the Garbage Collector itself, which does not store objects. Option B, Handle, does not provide the necessary memory management features for this scenario, and Option C, Cache, is not designed to manage memory in the way required here.