Click here to Skip to main content
13,357,973 members (60,533 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 31 Mar 2011

.NET Double Reference

, 31 Mar 2011
Rate this:
Please Sign up or sign in to vote.
Simple class to encapsulate a double reference


The DoubleReference class below wraps a reference which gives one reference to references. When used, they act like double pointers in C and in some cases can increase efficiency a great deal.


Double pointers in C give one the ability to have objects point to a "placeholder". The placeholder can change value and all the objects pointing to the placeholder will see the change. The placeholder itself is usually a reference/pointer to the object of interest. This can be achieved similarly in .NET by wrapping a reference in a class.

Using the Code

Use the class...

public class DoubleReference<T>
    public T Reference { get; set; }
    public DoubleReference(T reference) { Reference = reference; }
    public static implicit operator T(DoubleReference<T> x) { return x.Reference; }
    public static T operator ~(DoubleReference<T> x) { return x.Reference; }
} you would any reference but simply wrap them with this class. You can provide a direct link to a reference but wrap it in a property of the original type. This allows complete transparency.

DoubleReference<Test2> reff;
public Test2 RootTest { get { return reff.Reference; } set { reff.Reference = value; } }

Here we have a double reference placeholder. RootTest simply wraps the double reference and returns whatever it points to. Alternatively, we could expose the double reference if we want to avoid possible issues that changes to RootTest might incur. We could also remove the setter for Root Test.

The idea here is that we can create a single DoubleReference class and pass that around. As long as objects that reference this object do not change their reference, any changes to the double reference's reference will "propagate" to all objects using it.

You can use this, for example, in a collection where every element in the collection has a "Current" property. The Current property points to some current element. When the value has to change, it should propagate to all elements. By using a double reference, you can do this instantaneously at the cost of the extra memory.


  • 29th March, 2011: Initial version


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhy the operator ~? Pin
Don Kackman31-Mar-11 11:44
memberDon Kackman31-Mar-11 11:44 
AnswerRe: Why the operator ~? Pin
Jon_Slaughter31-Mar-11 14:54
memberJon_Slaughter31-Mar-11 14:54 
GeneralRe: Why the operator ~? Pin
Don Kackman1-Apr-11 8:02
memberDon Kackman1-Apr-11 8:02 
GeneralRe: Why the operator ~? [modified] Pin
Jon_Slaughter1-Apr-11 9:39
memberJon_Slaughter1-Apr-11 9:39 
Lol, it's called operator overloading. You can change the behavior of many operators in most modern programming languages.

C# realizes that I want to change the behavior of ~ on objects of type DoubleReference so instead of using it's own internal code it gets the code in the class.

public static T operator ~(DoubleReference<T> x) { return x.Reference; }

Basically you can think of operators as functions. Unary are operators that take a single argument, binary = 2, ternary = 3, etc...

So when you do a + b the compiler translates that into Add(a,b).

Now in modern languages the type's can generally be inferred. So that if a is an int we call Int.Add(a,b) and if a is a class of type Dog we call Dog.Add(a,b).

Most reference types(classes, etc...) do not have operators defined on them. That is, Dog.Add(a,b) is undefined. But if add that method(using a special syntax) to the class the compiler will use it when adding two objects as long as the first one is of type Dog. If it is of type Cat it will look in Cat for the operator.

In any case just search operator overloading... tons of information on it.

modified on Friday, April 1, 2011 2:53 PM

GeneralRe: Why the operator ~? Pin
Don Kackman1-Apr-11 12:00
memberDon Kackman1-Apr-11 12:00 
GeneralRe: Why the operator ~? Pin
Jon_Slaughter1-Apr-11 15:09
memberJon_Slaughter1-Apr-11 15:09 
GeneralSingle-item arrays... Pin
supercat931-Mar-11 6:35
membersupercat931-Mar-11 6:35 
GeneralRe: Single-item arrays... Pin
Jon_Slaughter31-Mar-11 10:20
memberJon_Slaughter31-Mar-11 10:20 

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
Web04 | 2.8.180111.1 | Last Updated 31 Mar 2011
Article Copyright 2011 by Jon_Slaughter
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid