Click here to Skip to main content
13,041,923 members (77,117 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 21 Dec 2007

Generic WeakReference

, 22 Dec 2007
Rate this:
Please Sign up or sign in to vote.
A generic implementation of the WeakReference class


In the .NET Framework, there is no generic implementation of the System.WeakReference class. This means that runtime casting has to be done by the developer which is error prone and repetitive work. I did this simple implementation to rid this problem. Of course, it does not help with the overhead that boxing introduces since it's still stored in an object variable in the base class. Hopefully future releases of the framework will contain a real generic WeakReference.

Using the Code

Just cut the code snippet below and paste it into your project to use it.

/// <span class="code-SummaryComment"><summary></span>
/// Represents a weak reference, which references an object while still allowing
/// that object to be reclaimed by garbage collection.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><typeparam name="T">The type of the object that is referenced.</typeparam></span>
public class WeakReference<T>
    : WeakReference where T : class
    /// <span class="code-SummaryComment"><summary></span>
    /// Initializes a new instance of the WeakReference{T} class, referencing
    /// the specified object.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="target">The object to reference.</param></span>
    public WeakReference(T target)
        : base(target)
    { }
    /// <span class="code-SummaryComment"><summary></span>
    /// Initializes a new instance of the WeakReference{T} class, referencing
    /// the specified object and using the specified resurrection tracking.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="target">An object to track.</param></span>
    /// <span class="code-SummaryComment"><param name="trackResurrection">Indicates when to stop tracking the object. </span>
    /// If true, the object is tracked
    /// after finalization; if false, the object is only tracked
    /// until finalization.<span class="code-SummaryComment"></param></span>
    public WeakReference(T target, bool trackResurrection)
        : base(target, trackResurrection)
    { }
    protected WeakReference(SerializationInfo info, StreamingContext context)
        : base(info, context)
    { }
    /// <span class="code-SummaryComment"><summary></span>
    /// Gets or sets the object (the target) referenced by the
    /// current WeakReference{T} object.
    /// <span class="code-SummaryComment"></summary></span>
    public new T Target
            return (T)base.Target;
            base.Target = value;


  • 21st December, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sweden Sweden
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralNice work Pin
David McMinn29-Aug-08 5:41
memberDavid McMinn29-Aug-08 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Dec 2007
Article Copyright 2007 by RexNebular
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid