我在给 dotnet 的 runtime 仓库提PR时,小伙伴告诉我可以使用 TryAdd 方法减少判断,但是我修改这个代码发现 100 个自动化测试都失败了,都告诉我没有找到这个方法

在这个更改 https://github.com/dotnet/runtime/pull/37041 有小伙伴告诉我可以使用 TryAdd 方法减少判断,我添加之后发现差不多 100 个自动化测试都失败,提示下面代码

'Dictionary<string, string>' does not contain a definition for 'TryAdd' and no accessible extension method 'TryAdd' accepting a first argument of type

原因是 Microsoft.Extensions.Configuration.CommandLine 这个库使用了 dotnet standard 2.0 版本

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;</TargetFrameworks>
    <EnableDefaultItems>true</EnableDefaultItems>
  </PropertyGroup>

而 TryAdd 方法是在 .NET Standard 2.1 才添加的,也就是 2.0 是没有这个方法

Dictionary<TKey,TValue>.TryAdd(TKey, TValue) Method (System.Collections.Generic)


本文会经常更新,请阅读原文: https://dotnet-campus.github.io//post/dotnet-%E5%AD%97%E5%85%B8%E7%B1%BB%E6%89%BE%E4%B8%8D%E5%88%B0-TryAdd-%E6%96%B9%E6%B3%95.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 lindexi (包含链接: https://dotnet-campus.github.io/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系