There's an advice in C++: "Never Return a Reference to a Local Object", as below quoted from "C++ Primer":
"There's one crucially important thing to understand about returning a reference: Never return a reference to a local variable.
"When a function completes, the storage in which the local objects were allocated is freed. A reference to a local object refers to undefined memory after the function terminates. Consider the following function:
// Disaster: Function returns a reference to a local object
const string &manip(const string& s)
{
string ret = s;
// transform ret in some way
return ret; // Wrong: Returning reference to a local object!
}
"This function will fail at run time because it returns a reference to a local object. When the function ends, the storage in which ret resides is freed. The return value refers to memory that is no longer available to the program."
Question: so does this still apply to C#? or it doesn't matter as GC is introduced?