DEV Community

Discussion on: Read-once variable - R1V, ROV, ... - in C#

Collapse
 
bugmagnet profile image
Bruce Axtens • Edited

And then there's the property variant

using System;
using System.Diagnostics;

namespace r1v
{
    class Program
    {
        static void Main(string[] args)
        {
            var r1v = new R1V<string>("Hello World");
            Console.WriteLine(r1v.Get);
            Console.WriteLine(r1v.Get);
        }
    }

    public class R1V<Type>
    {
        private Type _value;

        public R1V(Type value) => _value = value;

        public Type Get
        {
            get
            {
                try
                {
                    return _value;
                }
                finally
                {
                    _value = default(Type);
                }
            }
        }
    }
}