The
this
reference, instead of a copy of the receiver, is always anin
parameter passed by reference to the member method. This optimization saves more copying when you use areadonly struct
.
这到底是什么意思?在构造函数中使用“this”时,普通结构是否总是制作副本?
最佳答案
当您将结构作为参数传递给函数时,它会复制整个结构并将其提供给函数。
因为您使用的是只读结构,您实质上是在 promise 它永远不会改变。
当您在传递给它的函数上使用 (in) 修饰符时,您基本上是在 promise 您根本不会更改结构。因此,作为引用(基本上是指针)传入是安全的,而不是防御性地复制整个内容。
关于C# 7.3 只读结构类型 "this"引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50919526/