gpt4 book ai didi

C# 7.3 只读结构类型 "this"引用

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:10 25 4
gpt4 key购买 nike

readonly struct type

The this reference, instead of a copy of the receiver, is always an in parameter passed by reference to the member method. This optimization saves more copying when you use a readonly struct.

这到底是什么意思?在构造函数中使用“this”时,普通结构是否总是制作副本?

最佳答案

当您将结构作为参数传递给函数时,它会复制整个结构并将其提供给函数。

因为您使用的是只读结构,您实质上是在 promise 它永远不会改变。

当您在传递给它的函数上使用 (in) 修饰符时,您基本上是在 promise 您根本不会更改结构。因此,作为引用(基本上是指针)传入是安全的,而不是防御性地复制整个内容。

关于C# 7.3 只读结构类型 "this"引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50919526/

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