Skip to content

NestedReference

Kal Ahmed edited this page Sep 26, 2016 · 1 revision

Home > References > NestedReference

NestedReference

NestedReference is a convenience type that helps to implement nested variable scoping in situations where recursion or modularization of code is not possible or desirable.

The reference will always retrieve the currently in-scope value based on the nesting level. This is defined to be the most recently set value in-scope for the current nesting level. The nesting level is controlled by calling the IncrementNesting() and DecrementNesting() methods, when a set value goes out of scope i.e. the nesting level is decrementing to less than the level it was set at then that value is lost and the value reverts to the most recently set value prior to that nesting level.

A worked example serves to illustrate the behaviour:

//Create a new reference
NestedReference<String> ref = new NestedReference<String>();
//The initial value is currently null

//We now set a value
ref.Value = "one";

//Increment nesting, the value is still "one"
ref.IncrementNesting();

//Now set a value at this nesting level
ref.Value = "two";

//The value is now "two"

//We now decrement nesting, this means the value "two" goes out of
//scope and we revert to the previous value
ref.DecrementNesting();

//The value is now "one"

//If we decrement nesting again the value "one" goes out of scope
ref.DecrementNesting();

//The value is now null again

As can be seen in the example the API is very simple, a Value property provides get and set access to the currently in-scope reference and the IncrementNesting() and DecrementNesting() methods control the nesting level.

Clone this wiki locally