Search code examples
c#genericsvalue-typereference-type

Best way to use Value types as Reference types?


I've been using something similar to this whenever I needed to reference a Value type:

    public class RefHost<T> {
        public RefHost(T val)
        {
            Value = val;
        }
        private T _value;
        public T Value {
            get {
                return _value;
            }
            set {
                _value = value;
            }
        }
    }

What I'm wondering is there a built in way or an easier way to use an existing Value type as a Reference type?

Example:

public class Editor {
    public RefHost<int> Blah = new RefHost<int>(5);

    // Some kind of timer to increase the value of Blah every few ticks
}

Kind of like that where the user of Editor specifies a value type that needs to be changed, and there can be multiple instances of Editor each with it's own value. I used the timer as an example but most of the time it's a user control like a slider.


Solution

  • I think you are looking for Tuple.