Skip to content

Update a Dictionary Value in C#: .NET 10, TryAdd, GetValueRefOrAddDefault and AddOrUpdate samples - #2272

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/79087-csharp-update-the-value-stored-in-a-dictionary
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/79087-csharp-update-the-value-stored-in-a-dictionary

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample for https://code-maze.com/csharp-update-the-value-stored-in-a-dictionary/

Changes, all inside collections-dictionary/UpdateDictionaryValue:

  • Both projects retargeted from net7.0 to net10.0.
  • Test project moved from MSTest.TestAdapter + MSTest.TestFramework 2.2.10 to the MSTest meta package. The folder only uses Assert.AreEqual, so no test body changed for the MSTest 4 rename of Assert.ThrowsException.
  • ToppingsDictionary.Toppings is now get-only ({ get; } instead of { get; private set; }).
  • New AddToppingIfMissing (Dictionary.TryAdd) and AddToppingsByRef (CollectionsMarshal.GetValueRefOrAddDefault), as printed in the article.
  • Program.cs prints the same sequence as the article's output blocks (olive = 3 in both runs), with the "jalepeno" spelling corrected to "jalapeno", and adds the ConcurrentDictionary.AddOrUpdate demonstration (prints 4, then 8).
  • Three new tests, one per new code item (5 tests total).

Package versions, re-queried on NuGet 2026-10-09 (latest listed stable):

  • Microsoft.NET.Test.Sdk 18.10.1
  • MSTest 4.5.1
  • coverlet.collector 10.1.0

Local checks on SDK 10.0.302 / runtime 10.0.10: dotnet build -c Release 0 warnings 0 errors; dotnet test 5 passed, 0 failed; dotnet list package --vulnerable --include-transitive: no vulnerable packages in either project.

…ult, AddOrUpdate

Retarget both projects to net10.0, move to the MSTest 4.5.1 meta package, make Toppings get-only, add AddToppingIfMissing (TryAdd) and AddToppingsByRef (CollectionsMarshal.GetValueRefOrAddDefault), fix the jalapeno spelling, make Program.cs print the article's output sequence, and add a ConcurrentDictionary.AddOrUpdate demonstration. Three new tests (5 total).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant