From b2187bc5ed79e44f700e65ecaf4ce41fce84a235 Mon Sep 17 00:00:00 2001 From: sakno Date: Wed, 14 Aug 2024 22:33:24 +0300 Subject: [PATCH] Added support of anonymous value reference --- src/DotNext.Tests/Runtime/ValueReferenceTests.cs | 10 ++++++++++ src/DotNext/Runtime/ValueReference.cs | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/DotNext.Tests/Runtime/ValueReferenceTests.cs b/src/DotNext.Tests/Runtime/ValueReferenceTests.cs index 0016f733f..a38fb6f09 100644 --- a/src/DotNext.Tests/Runtime/ValueReferenceTests.cs +++ b/src/DotNext.Tests/Runtime/ValueReferenceTests.cs @@ -94,6 +94,16 @@ public static void ImmutableEmptyRef() Null(reference.ToString()); } + [Fact] + public static void AnonymousValue() + { + var reference = new ValueReference(42); + Equal(42, reference.Value); + + ReadOnlyValueReference roRef = reference; + Equal(42, roRef.Value); + } + private record class MyClass : IResettable { internal int Field; diff --git a/src/DotNext/Runtime/ValueReference.cs b/src/DotNext/Runtime/ValueReference.cs index e5f540fd4..6819f349b 100644 --- a/src/DotNext/Runtime/ValueReference.cs +++ b/src/DotNext/Runtime/ValueReference.cs @@ -28,6 +28,20 @@ public ValueReference(T[] array, int index) { } + private ValueReference(StrongBox box) + : this(box, ref box.Value!) + { + } + + /// + /// Creates a reference to the anonymous value. + /// + /// The anonymous value. + public ValueReference(T value) + : this(new StrongBox { Value = value }) + { + } + /// /// Gets a value indicating that is reference is empty. ///