Dispose method

  • Thread starter Thread starter web1110
  • Start date Start date
Jon said:
Your reasoning is flawed - there's a big difference between passing a
reference by value and passing a value by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

And yes, sometimes it is worth passing reference-type parameters by
reference.

I do this in a web service I wrote where I pass in a SqlDataReader to
another method, by reference, because I want to close the data reader in
the called method.
 
You don't need to pass it by reference to close it in the other method. Both the caller reference and the received parameter reference will refer to the same object anyway.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Your reasoning is flawed - there's a big difference between passing a
reference by value and passing a value by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html

And yes, sometimes it is worth passing reference-type parameters by
reference.

I do this in a web service I wrote where I pass in a SqlDataReader to
another method, by reference, because I want to close the data reader in
the called method.
 
web1110 said:
I looked over your link and found it informative.

There is one refeence example in the link:
StringBuilder first = new StringBuilder();
StringBuilder second = first;
first.Append ("hello");
first = null;
Console.WriteLine (second);

How does GC know when the object is eligable for disposal? The instaniating
variable has dereferenced the object and another variable now exists. If
second dereferences the object so it is no longer in use, how is this
condition detected?

The GC is able to tell when it runs what the "root" references are -
static variables, locals in all stack frames etc. It then walks down
the variables that the objects those variables refer to contain, etc.

See http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx
 
John Bailo said:
I do this in a web service I wrote where I pass in a SqlDataReader to
another method, by reference, because I want to close the data reader in
the called method.

That suggests that you don't really understand the difference between
pass-by-reference and passing a reference by value then, I'm afraid...
 
Alex said:
That's what I meant.
Saying that the reference to the object is passed by value is the
same as saying that the object itself is passed by reference.

No it's *not*. See http://www.pobox.com/~skeet/csharp/parameters.html
What I did not know is whether there are benefits to passing the
reference by reference (apart from being able to change it so it
references another object).

That's the whole *purpose* of passing a parameter by reference (unless
it's a large value type).
Is that the only benefit?

The reason that I'm asking is that I see a lot of C# code on the net
that passes object references by reference to functions that do not
change the reference (but may change the objects themselves). I
thought that I may be missing something...

That's likely to just be bad code.
 
Jon Skeet said:

Yes it *is*.
Pointing to a web site without understanding what it says does not prove anything. Nor does using asterisks.

Jon's sidenote talks about: "what is the difference between passing a value object by reference and a reference object by value?"
Which is distinctly different from what I said.
That's likely to just be bad code.

Thank you for confirming that.

Best wishes,
Alex.
 
Alex said:
Yes it *is*.
Pointing to a web site without understanding what it says does not
prove anything. Nor does using asterisks.

Um, I wrote that article. I definitely do understand what it says. If
you're claiming to agree with the article and still maintain that C#'s
behaviour is "the same as saying that the object itself is passed by
reference" then you're the one who doesn't understand the article, I'm
afraid.
Jon's sidenote talks about: "what is the difference between passing a
value object by reference and a reference object by value?" Which is
distinctly different from what I said.

Not really. If you think it is, maybe I should change the article to
make it clearer. Passing an object itself by any means isn't possible
in .NET, as no expression has a type which is the object itself.

Pass by reference has a very specific meaning in terms of formal and
actual parameters, and is different to passing a reference by value -
in the same way as C doesn't have pass-by-reference at all, but that
doesn't stop you from passing pointers (by value).
 
Jon Skeet said:
Um, I wrote that article. I definitely do understand what it says. If
you're claiming to agree with the article and still maintain that C#'s
behaviour is "the same as saying that the object itself is passed by
reference" then you're the one who doesn't understand the article, I'm
afraid.

Maybe I don't.

Would you be so kind to explain the difference between the following statements:

1) Object X is passed to function F by reference.
2) A reference to Object X is passed to function F by value.

I claim that the statements are equivalent, as they both describe putting a reference to object X on the stack (or a register, whatever) before calling function F. The difference is semantic but it can help visualize what is going on.

If you think that I am mistaken, please show me how.


Your article, as I understand it, talks about two different types of objects - a value object that is passed by reference and a reference object that is passed by value. In that case the behaviour that you are trying to show is indeed different but that is something different altogether.
Not really. If you think it is, maybe I should change the article to
make it clearer. Passing an object itself by any means isn't possible
in .NET, as no expression has a type which is the object itself.

My mistake. I was using generic English terms and not specific .NET terminology.
If I replace "object" with "entity" will you agree with my statement then?
Pass by reference has a very specific meaning in terms of formal and
actual parameters, and is different to passing a reference by value -
in the same way as C doesn't have pass-by-reference at all, but that
doesn't stop you from passing pointers (by value).

Actually, passing a pointer in C is equivalent to "pass by reference".
And the actual behaviour is pretty close to your example:
Passing a pointer to a data type (I'm being careful not to say "object") allows you to change the value if that data type from inside the function, but *not* to "re-seat" the pointer (make it point to a different instance of that data type.

The same principle holds with C++ references.

C# (and, if I recall correctly, Java) confuses matters a bit by only allowing "reference types" to reside on the heap and "value types" on the stack but there is no fundamental difference in behaviour.


Best wishes,
Alex.
 
Alex said:
Maybe I don't.

Would you be so kind to explain the difference between the following statements:

1) Object X is passed to function F by reference.
2) A reference to Object X is passed to function F by value.

Sure. Changing the value of the actual parameter in the function F has
no effect (as far as the caller sees) in the second case, but changes
the value of the formal parameter in the first case.
I claim that the statements are equivalent, as they both describe
putting a reference to object X on the stack (or a register,
whatever) before calling function F. The difference is semantic but
it can help visualize what is going on.

If you think that I am mistaken, please show me how.

See above.
Your article, as I understand it, talks about two different types of
objects - a value object that is passed by reference and a reference
object that is passed by value. In that case the behaviour that you
are trying to show is indeed different but that is something
different altogether.

While there are value types and reference types in .NET, it doesn't
really alter anything. If you pass something by value, then changing
the value which is passed doesn't affect the caller's value. If you
pass something by reference, the variable used by the called method is
the same variable (or l-value, whatever) as the caller is using -
changing one changes the other.
My mistake. I was using generic English terms and not specific .NET terminology.
If I replace "object" with "entity" will you agree with my statement then?

Nope, because it's the "pass by reference" which has a very specific
meaning. By claiming that objects are passed by reference, you're
implying semantics which just don't occur in .NET.
Actually, passing a pointer in C is equivalent to "pass by reference".

No it's not - that's the point. It's not the same thing in the strict
sense of the terminology, which means that if you tell people who *do*
know the strict sense of the terminology that objects are passed by
reference, they'll expect behaviour other than what they get.

Many of the uses of pass by reference can be simulated by passing an
address/reference/pointer/whatever by value, but it's not the same as
pass by reference in itself.
And the actual behaviour is pretty close to your example:
Passing a pointer to a data type (I'm being careful not to say
"object") allows you to change the value if that data type from
inside the function, but *not* to "re-seat" the pointer (make it
point to a different instance of that data type.

Changing the value of the pointer (the parameter) doesn't change the
caller's value though - it's only changing the value of what the
pointer points at that makes the change visible. The parameter here is
the pointer, not the value of the variable whose address is passed as a
pointer.
The same principle holds with C++ references.

I don't like to use C++ terminology as I don't know it well enough to
be confident of being strictly correct.
C# (and, if I recall correctly, Java) confuses matters a bit by only
allowing "reference types" to reside on the heap and "value types" on
the stack but there is no fundamental difference in behaviour.

Even that's not true, as value types also reside on the heap within
reference type objects. Anyway...

See http://www.yoda.arachsys.com/java/passing.html for another way of
putting things.
 
1) Object X is passed to function F by reference.
The actual reference is passed in - not possible in C
Thus the external reference CAN be modified
2) A reference to Object X is passed to function F by value.
A COPY of the reference is passed in - this is similar to "pass by
reference" in C
Thus the external reference CANNOT be modified


Try this Example:
void ByValue(X x)
{
x = null;
}

void ByRef(ref X x)
{
x = null;
}

void Test()
{
X x = new X();
ByValue(x);
Console.WriteLine("X is {0}", (x == null)? "null" : "assigned");
ByRef(ref x);
Console.WriteLine("X is {0}", (x == null)? "null" : "assigned");
}

*****OUTPUT******
X is assigned
X is null


This was explained on Jon's page

Hope this helps
Bill
 
Back
Top