No, there's no risk here. The reference will be returned, and if the garbage collector runs it will be able to see the reference in main
, so it won't free the string.
Note that even when the string is eligible for garbage collection, it won't be freed immediately - only when the garbage collector next runs (or even later, depending on what generation it ends up in).
Note that the garbage collector can collect objects which won't be used any more, even if there's a variable still in scope - so long as it knows that the variable won't be read again:
object x = new object();
Console.WriteLine(x);
// Lots more code not touching x
// The object could be collected at any time here