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 3843124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:53:34+00:00 2026-05-19T15:53:34+00:00

Possible Duplicates: Why use ref keyword when passing an Object? When to pass ref

  • 0

Possible Duplicates:
Why use ref keyword when passing an Object?
When to pass ref keyword in

What is the correct usage of the ‘ref’ keyword in C#. I believe there has been plenty of discussion threads on this, but what is not clear to me is:

  1. Is the ref keyword required if you are passing in a reference object? I mean when you create an object in the heap, is it not always passed by reference. Does this have to be explicitly marked as a ref?
  • 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-19T15:53:35+00:00Added an answer on May 19, 2026 at 3:53 pm

    Using ref means that the reference is passed to the function.

    The default behaviour is that the function receives a new reference to the same object. This means if you change the value of the reference (e.g. set it to a new object) then you are no longer pointing to the original, source object. When you pass using ref then changing the value of the reference changes the source reference – because they are the same thing.

    Consider this:

    public class Thing
    {
        public string Property {get;set;}
    }
    
    public static void Go(Thing thing)
    {
        thing = new Thing();
        thing.Property = "Changed";
    }
    
    public static void Go(ref Thing thing)
    {
        thing = new Thing();
        thing.Property = "Changed";
    }
    

    Then if you run

    var g = new Thing();
    
    // this will not alter g
    Go(g);
    
    // this *will* alter g
    Go(ref g);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: What to use: var or object name type? Use of var keyword
Possible Duplicates: Why use pointers? Passing a modifiable parameter to c++ function Why would
Possible Duplicates: When pass-by-pointer is preferred to pass-by-reference in C++? Are there benefits of
Possible Duplicate: When to use “strictfp” keyword in java? What is the use of
Possible Duplicates: Should I use public properties and private fields or public fields for
Possible Duplicates: Valid use of goto for error management in C? Examples of good
Possible Duplicates: Is excessive use of this in C++ a code smell Years ago,
Possible Duplicates: When do you use reflection? Patterns/anti-patterns What exactly is Reflection and when
Possible Duplicates: How do I use that “<<<HTML” thing in PHP? PHP <<<EOB $sql
Possible Duplicates: Why use ++i instead of i++ in cases where the value is

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.