garbage collection - from C++ to C#: Never Return a Reference to a Local Object? -


there's advice in c++: "never return reference local object", below quoted "c++ primer":

"there's 1 crucially important thing understand returning reference: never return reference local variable.

"when function completes, storage in local objects allocated freed. reference local object refers undefined memory after function terminates. consider following function:

 // disaster: function returns reference local object  const string &manip(const string& s)  {       string ret = s;       // transform ret in way       return ret; // wrong: returning reference local object!  } 

"this function fail @ run time because returns reference local object. when function ends, storage in ret resides freed. return value refers memory no longer available program."

question: still apply c#? or doesn't matter gc introduced?

it not possible return references local variables in .net

see: why doesn't c# support return of references?

see also: ref returns , ref locals (eric lippert's blog)


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -