M
M
mletov2018-03-19 13:22:42
.NET
mletov, 2018-03-19 13:22:42

Is the ref keyword required to pass by reference?

I want to change the object by passing it to the input inside the method, respectively, I wrote
private void MyMethod(ref XElement nd);
But then in some method I accidentally forgot and wrote without Ref, while the object inside the method changed as it should
private void MyMethod(XElement nd);
. Is ref not required for meaningful types? Explain, please, subtleties.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cicatrix, 2018-03-19
@mletov

Without the ref (out) keyword, the parameter is always passed by value.
The language has reference types and value types (reference type and value types) - here it is explained what is the difference.
XElement is a reference type. You pass the address of the object (in memory), in other words, a pointer to the object, in other words, a reference. Of course, when manipulating this area of ​​memory, the object itself also changes.
To answer your question in the title: for reference types, no, not necessarily; for value types, it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question