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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:23:06+00:00 2026-05-10T17:23:06+00:00

If I am passing an object to a method, why should I use the

  • 0

If I am passing an object to a method, why should I use the ref keyword? Isn’t this the default behaviour anyway?

For example:

class Program {     static void Main(string[] args)     {         TestRef t = new TestRef();         t.Something = 'Foo';          DoSomething(t);         Console.WriteLine(t.Something);     }      static public void DoSomething(TestRef t)     {         t.Something = 'Bar';     } }   public class TestRef {     public string Something { get; set; } } 

The output is ‘Bar’ which means that the object was passed as a reference.

  • 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. 2026-05-10T17:23:07+00:00Added an answer on May 10, 2026 at 5:23 pm

    Pass a ref if you want to change what the object is:

    TestRef t = new TestRef(); t.Something = "Foo"; DoSomething(ref t);  void DoSomething(ref TestRef x) {   x = new TestRef();   x.Something = "Not just a changed TestRef, but a completely different TestRef object"; } 

    After calling DoSomething, t does not refer to the original new TestRef, but refers to a completely different object.

    This may be useful too if you want to change the value of an immutable object, e.g. a string. You cannot change the value of a string once it has been created. But by using a ref, you could create a function that changes the string for another one that has a different value.

    It is not a good idea to use ref unless it is needed. Using ref gives the method freedom to change the argument for something else, callers of the method will need to be coded to ensure they handle this possibility.

    Also, when the parameter type is an object, then object variables always act as references to the object. This means that when the ref keyword is used you’ve got a reference to a reference. This allows you to do things as described in the example given above. But, when the parameter type is a primitive value (e.g. int), then if this parameter is assigned to within the method, the value of the argument that was passed in will be changed after the method returns:

    int v = 1; Change(ref v); Debug.Assert(v == 5); WillNotChange(v); Debug.Assert(v == 5); // Note: v doesn't become 10  void Change(ref int x) {   x = 5; }  void WillNotChange(int x) {   x = 10; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sounds good to integrate your CMS with your own Hub.… May 12, 2026 at 11:01 am
  • Editorial Team
    Editorial Team added an answer Edit: Just pointing out that this question, and its answers,… May 12, 2026 at 11:01 am
  • Editorial Team
    Editorial Team added an answer Something along these lines: insert into popular_tags (id, `count`) select… May 12, 2026 at 11:01 am

Related Questions

My iPhone application needs to submit an object to an already existing .NET SOAP
Instead of passing many arguments to a method, I was encapsulating it into an
I have a fairly large CRUD WinForm app that has numerous objects. Person, Enrollment,
MS Access allows the numeric type GUID (in German it's called 'Replikations-ID', so I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.