in-parameters

Using C# 7.2 in modifier for parameters with primitive types

孤人 提交于 2019-12-18 03:59:08
问题 C# 7.2 introduced the in modifier for passing arguments by reference with the guarantee that the recipient will not modify the parameter. This article says: You should never use a non-readonly struct as the in parameters because it may negatively affect performance and could lead to an obscure behavior if the struct is mutable What does this mean for built-in primitives such as int , double ? I would like to use in to express intent in code, but not at the cost of performance losses to

Using C# 7.2 in modifier for parameters with primitive types

蓝咒 提交于 2019-11-30 12:38:15
C# 7.2 introduced the in modifier for passing arguments by reference with the guarantee that the recipient will not modify the parameter. This article says: You should never use a non-readonly struct as the in parameters because it may negatively affect performance and could lead to an obscure behavior if the struct is mutable What does this mean for built-in primitives such as int , double ? I would like to use in to express intent in code, but not at the cost of performance losses to defensive copies. Questions Is it safe to pass primitive types via in arguments and not have defensive copies