Like Tree3Likes
  • 2 Post By nvoigt
  • 1 Post By Mario F.

Pass by reference question..

This is a discussion on Pass by reference question.. within the C# Programming forums, part of the General Programming Boards category; Scannig through a program a simple program at the moment but need to understand "pass by rederence" to coomplete it... ...

  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,139
    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
    Portugal
    Posts
    7,436
    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.
    rags_to_riches likes this.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 12:06 PM
  4. pass by reference question
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 11-30-2003, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21