Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:33:21+00:00 2026-05-23T14:33:21+00:00

There’s an advice in C++: Never Return a Reference to a Local Object, as

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:33:21+00:00Added an answer on May 23, 2026 at 2:33 pm

    It is not possible to return references to local variables in .NET

    See: Why doesn’t C# support the return of references?

    See also: Ref returns and ref locals (Eric Lippert’s Blog)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's a few previous questions on StackOverflow questioning how one goes about accessing local
There is one table T ( id integer, primary key ( id). I want
There are several shading languages available today like GLSL, HLSL, CG, which one to
There will be 500+ threads concurrently uploading an unique object to a bucket all
There is a component that I have been using since IE7 and had never
There are many tutorials that talk about deleting index.php from the url. But I
There are lots of posts on here about moving a folder out of one
There is a constant I'm using from the iOS SDK. I printed out the
There are two layers (Image control with PNG bitmap) inside Grid (or Canvas): one
There is no doubt that MonoTouch is one of the great cross-compiler(s). Similarly, SenchaTouch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.