G
Guest
Hi,
Could someone clarify my confusion regarding passing reference types to a
method with ref keyword and explain when it's practical to use it.
It's my understanding that in .NET reference types hold a reference to an
object as opposed to object data itself. So, when reference type parameter is
passed into a method, a copy of objects reference is passed in, so called
method can do whatever to "original" object and a caller will see those
changes.
But if one adds "ref" keyword the actual address of a variable that holds
object's reference is passed in, Which accomplishes the same thing as above
scenario (without ref), but far more dangerous. My questions:
1. Is my overall understanding accurate?
2. What are some practical (real life) usages for passing reference type
with ref.
3. Is there any performance benefits ref vs. without ref.
Thanks
Could someone clarify my confusion regarding passing reference types to a
method with ref keyword and explain when it's practical to use it.
It's my understanding that in .NET reference types hold a reference to an
object as opposed to object data itself. So, when reference type parameter is
passed into a method, a copy of objects reference is passed in, so called
method can do whatever to "original" object and a caller will see those
changes.
But if one adds "ref" keyword the actual address of a variable that holds
object's reference is passed in, Which accomplishes the same thing as above
scenario (without ref), but far more dangerous. My questions:
1. Is my overall understanding accurate?
2. What are some practical (real life) usages for passing reference type
with ref.
3. Is there any performance benefits ref vs. without ref.
Thanks