Return a ref from a function

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

    Return a ref from a function

    How do I return a references to an object from a function?

    static ArrayList getItemFile(str ing destdir){...}

    Does the above code return the whole Arraylist on the stack or does it
    return a reference?
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Return a ref from a function

    hi,

    it does return the reference, in the stack is kept the reference to the heap
    space occupied by the object. so when returned the caller will have the
    reference to the heap.



    cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation



    "Arne" <Arne@discussio ns.microsoft.co m> wrote in message
    news:200E4288-C939-4BDC-B057-06960C85819B@mi crosoft.com...[color=blue]
    > How do I return a references to an object from a function?
    >
    > static ArrayList getItemFile(str ing destdir){...}
    >
    > Does the above code return the whole Arraylist on the stack or does it
    > return a reference?[/color]


    Comment

    • Stoitcho Goutsev \(100\) [C# MVP]

      #3
      Re: Return a ref from a function

      Hi Arne,

      It depends whether the returned object is of value type or reference type.
      All types decalred using the keyword *class* are reference types as such all
      object returned as from methods or used as parameters are just references.
      All types that inherit from ValueType class are value types and the object
      itself is copied when instance of such type is. Value types are all types
      declared using *struct* or *enum* keywords, which means that in your sample
      getItemFile method will return a reference to the object rather than the
      object istelf.

      I'd suggest consulting the docs for the difference between value and
      reference types as well as reading about boxing/unboxing value types.


      --
      HTH
      Stoitcho Goutsev (100) [C# MVP]

      If you look in MSDN you'll see that ArrayList is decalred as a *class*
      "Arne" <Arne@discussio ns.microsoft.co m> wrote in message
      news:200E4288-C939-4BDC-B057-06960C85819B@mi crosoft.com...[color=blue]
      > How do I return a references to an object from a function?
      >
      > static ArrayList getItemFile(str ing destdir){...}
      >
      > Does the above code return the whole Arraylist on the stack or does it
      > return a reference?[/color]


      Comment

      Working...