This should be a fairly common question, but I haven’t found a straightforward answer anywhere.
If I instantiate an object within a function in VB.NET and return it, does it return it by reference or by value. IE – should I be worried about performance if I write something like this:
Public Function ret_obj_func() As big_object
Dim ret_obj As New big_obj(<lots of stuff>)
Return ret_obj
End Function
If I call this function from somewhere else, will it instantiate the object in the ret_obj and then create a deep copy to pass back a copy to the caller, Or will it just pass back a reference?
It just passes back a reference (assuming
big_objis a class). I wouldn’t use the term “by reference” here, as that has a subtly different meaning when it comes to parameter passing – but assumingbig_objis a class – a reference type – the value ofret_objis a reference, and that reference will be what’s returned.I don’t have any articles on this from a VB perspective, but if you’re happy to look at C#, you may find these articles useful: