简体   繁体   中英

C# 7.3 readonly struct type “this” reference

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 .

What exactly does this mean? Does normal struct always make a copy when using "this" in the constructor?

When you pass a struct as a parameter into a function, it makes a copy of the entire struct and gives it to the function.

Because you're using a readonly struct , you're essentially promising that it will never change.

When you use the (in) modifier on a function you pass it to, you're basically promising that you won't change the struct at all. So, it's safe to pass in as a reference (a pointer, basically), instead of copying the entire thing defensively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM