Return a ref from a function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

static ArrayList getItemFile(string destdir){...}

Does the above code return the whole Arraylist on the stack or does it
return a reference?
 
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,
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top