basic param passing question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver

    #1

    basic param passing question

    I'm an expert C# programmer (honest) but...

    the following code prints 'Oranges', not 'ORANGES' - this confuses me
    because I thought reference types (eg string) where passed
    by...err..refer ence - so changing where the reference points to in a
    method ought to change the behaviour outside the method.

    ....using the 'ref' keyword makes this happen.
    it's almost as if references passed to methods are *copied*.

    so - what's going on here, and why aren't all reference types passed
    by reference by default....?

    thanks,
    Oliver.



    class Class1
    {

    static void Main(string[] args)
    {
    string fruit = "Oranges";
    DoSomeWork(frui t);
    Console.WriteLi ne("fruit: " + fruit);
    }

    private static void DoSomeWork(stri ng myString)
    {
    string newString = myString.ToUppe r();
    myString = newString;
    }

    }
  • Jon Skeet [C# MVP]

    #2
    Re: basic param passing question

    Oliver <spacehopper_ma n@yahoo.com> wrote:[color=blue]
    > I'm an expert C# programmer (honest) but...
    >
    > the following code prints 'Oranges', not 'ORANGES' - this confuses me
    > because I thought reference types (eg string) where passed
    > by...err..refer ence - so changing where the reference points to in a
    > method ought to change the behaviour outside the method.[/color]

    No. Reference type values are passed by value by default, just as value
    type values are passed by value by default. It's just that a reference
    type value *is* a reference, not a full-blown object itself.

    See http://www.pobox.com/~skeet/csharp/parameters.html
    [color=blue]
    > ...using the 'ref' keyword makes this happen.[/color]

    Yup.
    [color=blue]
    > it's almost as if references passed to methods are *copied*.[/color]

    It's not almost as if, that's exactly what happens.
    [color=blue]
    > so - what's going on here, and why aren't all reference types passed
    > by reference by default....?[/color]

    Why should they be? Personally I think that would cause far more
    confusion than the current system - especially if value types were
    still passed by value by default. Do you really *want* every method to
    have to declare that a string parameter should be passed by value, just
    so that the caller can rely on its own variable's value not being
    changed?

    (Also note that there are type problems in terms of passing things by
    reference - you can't pass the a string variable by reference to a
    method declared as taking an object, for instance, because the method
    could very well set the value to something other than a string, which
    would break type safety.)

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...