1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

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

View File

@ -54,6 +54,8 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ref var value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); 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; return value = exists ? updateValueFactory(key, value!) : addValue;
#else #else
if (dictionary.TryGetValue(key, out TValue? old)) if (dictionary.TryGetValue(key, out TValue? old))
@ -173,6 +175,8 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); 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); return value = exists ? updateValueFactory(key, value!) : addValueFactory(key);
#else #else
if (dictionary.TryGetValue(key, out TValue? old)) if (dictionary.TryGetValue(key, out TValue? old))
@ -310,6 +314,8 @@ public static class DictionaryExtensions
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); 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); return value = exists ? updateValueFactory(key, value!, factoryArgument) : addValueFactory(key, factoryArgument);
#else #else
if (dictionary.TryGetValue(key, out TValue? old)) if (dictionary.TryGetValue(key, out TValue? old))