locked
Array variables as method parameters RRS feed

  • Question

  • It seems to me that array variables, by their nature, can only be passed to a method by reference, and so the keyword "ref" becomes unnecessary. Indeed, I tried this out in my application, and adding "ref" seems to make no difference in the behavior: whether I do or not, the method returns with the value of the variable changed. However, I have not been able to find this fact clearly stated in any documentation. Could someone confirm that my understanding is correct?
    Sunday, March 18, 2007 12:54 PM

Answers

  • Only intergral types and structs are passed by value, an array is really just a class so it is passed by reference. 

    theTroll


    Sunday, March 18, 2007 1:50 PM
  • It's the same as passing any reference type.  You need 'ref' only if you want the method to pass back a different array.  The contents of the array can by modified either way since what is being passed (the managed pointer to the object) is identical whether it's 'ref' or not.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: C# to Python converter, VB to Python converter

    Sunday, March 18, 2007 1:52 PM

All replies

  • Only intergral types and structs are passed by value, an array is really just a class so it is passed by reference. 

    theTroll


    Sunday, March 18, 2007 1:50 PM
  • It's the same as passing any reference type.  You need 'ref' only if you want the method to pass back a different array.  The contents of the array can by modified either way since what is being passed (the managed pointer to the object) is identical whether it's 'ref' or not.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: C# to Python converter, VB to Python converter

    Sunday, March 18, 2007 1:52 PM