Using the GetValueOrDefault(…) function on a Dictionary, will the default execute always or only if result is null?

huangapple go评论71阅读模式
英文:

Using the GetValueOrDefault(...) function on a Dictionary, will the default execute always or only if result is null?

问题

我有一个缓存字典,对于这种情况,GetValueOrDefault(...)非常适用。

var myThing = _cache.GetValueOrDefault("key", new Thing());

我在文档中找不到默认值是否总是执行,还是只有在字典返回null时才执行。这很重要,因为在我的情况下,构造Thing会对性能产生显著影响(这是一个游戏),我只想在必要时构造它。

英文:

I have a cache dictionary and the GetValueOrDefault(...) is perfect for that case.

var myThing = _cache.GetValueOrDefault("key", new Thing());

I can't find in the docs if the default is always executed or only when the dictionary resolved null. This is important because in my case constructing Thing has a significant performance impact (it's a game) and I only want to construct it, if necessary.

答案1

得分: 5

它将始终被执行。 这是将值传递给函数的语义:首先计算该值,然后传递。

相反,您可以使用GetValueOrDefault的另一个重载以及空合并运算符??

var myThing = _cache.GetValueOrDefault("key") ?? new Thing();

只有在第一部分返回null时,才会执行第二部分。类型显然需要是可为空的,可以是引用类型或可空值类型。

英文:

It will always be executed. That is the semantics of passing a value to a function: the value will first be calculated and then passed.

Instead you could use the other overload of GetValueOrDefault along with a null-coalesce ??:

var myThing = _cache.GetValueOrDefault("key") ?? new Thing();

This will only execute the second half if the first half returns null. The type obviously needs to be nullable, so either a reference type or a nullable value type.

答案2

得分: 4

你可以使用TryGetValue方法,并按照以下方式使用:

// 我们尝试获取现有值,只有在没有值时才创建新的对象
var myThing = _cache.TryGetValue("key", out var existing) ? existing : new Thing();

请注意,在执行方法GetValueOrDefault时,.Net会计算参数,这就是为什么new Thing()(第二个参数)会被执行的原因。

英文:

You can use TryGetValue and put it as follow:

// We try to get existing value and only if we don't have it we create a new one
var myThing = _cache.TryGetValue("key", out var existing) ? existing : new Thing();

Please, note that when executing a method - GetValueOrDefault - in your case, .Net computes arguments, that's why new Thing() - the second argument - will be executed.

答案3

得分: 3

是的,它将被评估。这就是参数传递给方法的方式。在方法运行之前,必须评估所有的参数。

如果你不想让默认值不必要地被评估,你可以这样做:

_cache.GetValueOrDefault("key") ?? new Thing()

如果Thing是一个引用类型,其默认值为null,并且你要获取的字典中没有实际的null值,那么这将起作用。然后你可以使用??运算符来提供你的实际默认值。

与方法调用不同,如果左操作数非空,??运算符不会评估右操作数。

英文:

Yes, it will be evaluated. That is how parameters are passed to methods. All the parameters must be evaluated before the method can run.

If you don’t want the default value to be evaluated unnecessarily, you can do:

_cache.GetValueOrDefault("key") ?? new Thing()

This will work if Thing is a reference type, whose default value is null, and that there is no actual null values in the dictionary that you want to get. You then use the ?? operator to provide your actual default value.

Unlike method calls, the ?? operator does not evaluate the RHS if the LHS is non-null.

答案4

得分: -1

它将始终被执行。它将始终检查HasValue属性的值是false还是true。如果为true,它将返回特定的值,否则将返回默认值。

GetValueOrDefault方法会返回一个值,即使HasValue属性为false(与Value属性不同,后者会引发异常)。如果HasValue属性为false,该方法将返回基础类型的默认值。

英文:

It will be always executed. It is always going to check if the HasValue property is false or true. If it is true, It will return the specific value otherwise a default values will be returned.

> The GetValueOrDefault method returns a value even if the HasValue
> property is false (unlike the Value property, which throws an
> exception). If the HasValue property is false, the method returns the
> default value of the underlying type.

huangapple
  • 本文由 发表于 2023年7月31日 18:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定