简体   繁体   中英

Underlying implementation of ref for value types

I was curious how refs are created under the hood for structs . If I pass an non-value type as a ref parameter it makes sense that it's the reference to that object.

But for Value types I'm curious, does the run-time create an actual reference (with indirection) or the pointer to actual address

value types are copied, reference types are passed by reference. If you ref a struct you get a pointer to the struct. if you ref a reference type you get a pointer to the instance.

ref value types are useful for being able to change the value the calling function has. ref reference types are useful for being able to change which instance the calling function has.

Hood peeks:

Depending your performance needs you may not need to know any of this: https://blogs.msdn.microsoft.com/ericlippert/2009/04/27/the-stack-is-an-implementation-detail-part-one/

Reference types are created on the heap, Value types are usually created on the stack.

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-2024 STACKOOM.COM