gpt4 book ai didi

c# - 是三元运算符 (?:) thread safe in C#?

转载 作者:太空狗 更新时间:2023-10-29 20:00:02 26 4
gpt4 key购买 nike

考虑以下两种在 currentPrice100 之间获得更高数字的替代方法...

int price = currentPrice > 100 ? currentPrice : 100

int price = Math.Max(currentPrice, 100)

我提出这个问题是因为我在考虑 currentPrice 变量可以被其他线程编辑的上下文。

在第一种情况下... price 能否获得低于 100 的值?

我在考虑以下问题:

if (currentPrice > 100) {
//currentPrice is edited here.
price = currentPrice;
}

最佳答案

它不是线程安全的。

?: 只是普通 if 的快捷方式,因此您的 if 示例等同于 ? 一个 -如果此代码外没有锁定,您可以获得低于 100 的价格。

关于c# - 是三元运算符 (?:) thread safe in C#?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com