Click here to Skip to main content
Licence CPOL
First Posted 31 Mar 2011
Views 5,693
Downloads 77
Bookmarked 4 times

.NET Double Reference

Simple class to encapsulate a double reference
   4.33 (3 votes)

1

2

3
2 votes, 66.7%
4
1 vote, 33.3%
5
4.33/5 - 3 votes
μ 4.33, σa 1.01 [?]
 

Introduction

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.

Background

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; }
}        

...as 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.

History

  • 29th March, 2011: Initial version

License

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

About the Author

Jon_Slaughter



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhy the operator ~? PinmemberDon Kackman11:44 31 Mar '11  
AnswerRe: Why the operator ~? PinmemberJon_Slaughter14:54 31 Mar '11  
GeneralRe: Why the operator ~? PinmemberDon Kackman8:02 1 Apr '11  
GeneralRe: Why the operator ~? [modified] PinmemberJon_Slaughter9:39 1 Apr '11  
GeneralRe: Why the operator ~? PinmemberDon Kackman12:00 1 Apr '11  
GeneralRe: Why the operator ~? PinmemberJon_Slaughter15:09 1 Apr '11  
GeneralSingle-item arrays... Pinmembersupercat96:35 31 Mar '11  
GeneralRe: Single-item arrays... PinmemberJon_Slaughter10:20 31 Mar '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120222.1 | Last Updated 31 Mar 2011
Article Copyright 2011 by Jon_Slaughter
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid