Re: Using ref



On Jul 3, 7:33 am, "Steve Harclerode" <Camel.Software...@xxxxxxxxxxxx>
wrote:
I've read the page, but I still don't see how my languaging would be
considered different than "references are passed by value". What part of
what I wrote isn't correct?

When a parameter is passed by reference, that means that changes to
the parameter are reflected in the variable used as the argument. When
a reference is passed by value, that's not the case.

So consider this code:

public void Swap (object a, object b)
{
object tmp = a;
a = b;
b = a;
}

If the parameter were passed by reference by default, that would work.
As it is, it does nothng.

The *object* isn't actually passed at all, which is another reason
that "objects are passed by reference" is incorrect. In particular,
consider the case where the reference is actually null. What's being
passed and in what way at that point?

It's a clearer and more accurate mental model to talk about references
being passed by value. It avoids confusing people who understand what
"by reference" means, and would expect the Swap method above to work
when presented with your description. (I've seen this happen on
various occasions.) It also makes it very confusing when you add the
"ref" parameter for a reference type - if things were already being
passed by reference, what would adding "ref" do?

Jon
.



Relevant Pages

  • Re: Passing arguements by reference
    ... but doSomething doesnt change p unless i use "ref"? ... reference, hence both p and q pointing to the same object, not to different ... don't use the ref keyword. ... void Execute() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Documentation suggestions
    ... > Ian> I think it would be very useful if there was reference (not just ... Lang ref as only for "language lawyers". ...
    (comp.lang.python)
  • Re: pass by reference
    ... focus has moved from insisting to use "by ref" in java-context ... independent of any other use of the word "reference" in a language. ... Just because Java-refs can be re-targetted by assignment? ... If Java had an operator to tell an Object ...
    (comp.lang.java.programmer)
  • Re: ref parameter
    ... It's the same as in Java if you leave out the 'ref' - you can modify the internals of the object, but not the top-level reference. ... Instances of value types are passed as a copy by default, you can use the "ref" keyword to pass a reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Texas TMS1232 Delta Sigma converters
    ... John Larkin writes: ... >>picked up between the transducer and the ADC I'm trying to eliminate. ... the reference problem is serious. ... We're using an Analog Devices Xfet ref, ADR421, ...
    (sci.electronics.design)

Loading