Thread: Pass by reference question..

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Ireland
    Posts
    13

    Question Pass by reference question..

    Scannig through a program a simple program at the moment but need to understand "pass by rederence" to coomplete it... Could someone explain what this means??

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Lets say you have a text on a piece of paper.

    "pass by value" means you make a copy and give it to me. I can write notes on my piece of paper, but for obvious reasons they won't magically appear on your orginal sheet.

    "pass by reference" means you actually give that piece of paper to me. Anything I write on it, will still be on it when I give it back. As you don't need to make a copy, it's also faster.

    All reference types are "pass by reference" in C# while all value types are automatically "pass by value" so there is no syntax I could explain.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'd like to make a small clarification because the following is an usual source of confusion among beginners to the language:

    All reference types in C# (all types not derived from System.ValueType) are passed by reference, just as nvoigt points out. However they still can be passed by reference or by value with different results.

    Code:
    class foo {
        public int value;
    
        public foo(int val) { value = val; }
    }
    foo is a class type. As such it is a reference-type (not derived from System.ValueType, but from System.Object) and will always be passed by reference between caller and callee. So if I'm to write the following code, bar will indeed change:

    Code:
    void Main() {
      
        foo bar = new foo(13);
        Console.WriteLine(bar.value); // will print 13
        ChangeBarByVal(bar);
        Console.WriteLine(bar.value); // will print 31
    
    }
    
    static void ChangeBarByVal(foo f) { // foo being passed by-value
        f.value = 31;
    }
    If I change my code to pass by reference, the same thing will happen.

    Code:
    void Main() {
      
        foo bar = new foo(13);
        Console.WriteLine(bar.value); // will print 13
        ChangeBarByRef(ref bar);
        Console.WriteLine(bar.value); // will print 31
    
    }
    
    static void ChangeBarByRef(ref foo f) { // foo being passed by-ref
        f.value = 31;
    }
    The question thus is, why would I want to pass by ref, something that is always passed by ref? The real distinction between passing by val and by ref, when it comes to ref types is not in the ability to change the object state. This will always be guaranteed. But in the ability to reassign the object.


    Code:
    void Main() {
      
        foo bar = new foo(13);
    
        Console.WriteLine(bar.value); // will print 13
        ChangeBarByValue(bar);
        Console.WriteLine(bar.value); // will still print 13, not 31
        ChangeBarByRef(ref bar);
        Console.WriteLine(bar.value); // will print 65
    
    }
    
    static void ChangeBarByValue(foo f) { // foo being passed by-value
        f = new foo(31);
    }
    
    static void ChangeBarByRef(ref foo f) { // foo being passed by-ref
        f = new foo(65);
    }
    As you can see, passing by value won't allow you to reassign the object (change the instance to where your reference is pointing). But passing by ref will.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Const pass by reference question....
    By darren78 in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2010, 05:04 AM
  2. pass by reference question
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 09-11-2008, 07:00 PM
  3. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  4. pass by reference question
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 11-30-2003, 04:04 PM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM