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

  • SEARCH
  • Home
  • 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

Related Questions

I have a get method passing in an object of type Store to the
Objective-C uses a sophisticated message-passing system when one object calls a method on another
I am trying to use a business object that I am passing to the
Is there some kind of enumeration library or method I should use or should
I am passing 20+ arguments of different types to method. There should be some
I need to post a notification using postNotificationName:object:userInfo: method, and I'm passing a custom
I am passing an object from one asp.net page to another. I'm encoding the
I am passing one parameter/object to the function RefValTest(object oIn) by the value but
How can I instantiate an anonymous object while passing the propertynames and values as
Motivation Recently I searched for a way to initialize a complex object without passing

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.