docs: explain use of ref return

This commit is contained in:
Oliver Booth 2023-04-05 23:19:08 +01:00
parent 4a0e3c10d7
commit 8e6796607c
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
1 changed files with 6 additions and 0 deletions

View File

@ -54,6 +54,8 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER
ref var value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
// DO NOT CHANGE. reassigning value is necessary to mutate the dictionary, due to ref return above.
// mutation of the dictionary is INTENDED BEHAVIOUR. this is not a mistake.
return value = exists ? updateValueFactory(key, value!) : addValue;
#else
if (dictionary.TryGetValue(key, out TValue? old))
@ -173,6 +175,8 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
// DO NOT CHANGE. reassigning value is necessary to mutate the dictionary, due to ref return above.
// mutation of the dictionary is INTENDED BEHAVIOUR. this is not a mistake.
return value = exists ? updateValueFactory(key, value!) : addValueFactory(key);
#else
if (dictionary.TryGetValue(key, out TValue? old))
@ -310,6 +314,8 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
// DO NOT CHANGE. reassigning value is necessary to mutate the dictionary, due to ref return above.
// mutation of the dictionary is INTENDED BEHAVIOUR. this is not a mistake.
return value = exists ? updateValueFactory(key, value!, factoryArgument) : addValueFactory(key, factoryArgument);
#else
if (dictionary.TryGetValue(key, out TValue? old))