is it better to use:
Mesh mesh = Mesh GenerateMesh(){ ... }
or
void GenerateMesh(out mesh)
ie get an output or pass in the mesh
is it better to use:
Mesh mesh = Mesh GenerateMesh(){ ... }
or
void GenerateMesh(out mesh)
ie get an output or pass in the mesh
I would guess it’s more efficient and safe to use out or ref. If the variable mesh is already referring to a mesh object, the first example will them make a new mesh, and then assign it to the mesh variable, and thus there’s a potential risk of object leaking. If you’re using out, you can directly modify the existing one.
It’s almost always simpler to use return
unless you have a specific use-case scenario. out
is when you need 2+ returned variables and out
for a single variable just complicates code. And ref
is essentially for not copying value types and passing references/pointers (and Mesh
isn’t a value type, so it just “double-references” with no benefit). There is no performance benefit to using out
(see comment). In fact, MSDN recommends not using out
/ref
.