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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:47:58+00:00 2026-05-16T04:47:58+00:00

I looked at the similar questions and read some articles. THis article has some

  • 0

I looked at the similar questions and read some articles. THis article has some pictures which makes it clear.

SomeObject so = new SomeObject();
somefunction(so);
Console.write(so.x); // will print 1

SomeObject so1 = new SomeObject();
somefunctionByRef(so1);
Console.write(so1.x); // will print 1


static void somefunction(SomeObject so)
{
    so.x = 1;
}

public void somefunctionByRef(ref SomeObject so)
{
    so.x = 1;
}

Both of the methods have the same effect on this reference type. so why choose ref keyword for reference types?

is it a bad practice (possibly false) to use somefunction(SomeObject so) and modify the object inside the method and expect the changes without using the ref keyword?

  • 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-16T04:47:58+00:00Added an answer on May 16, 2026 at 4:47 am

    The default parameter passing in .Net is done by value. This is true for both value types and reference types. The difference is that with a reference type you are passing a reference to the instance by value vs. the actual object.

    The effect is the so reference in the original function and someFunction are independent. Changing which instance the reference refers to has no affect on the other. However because they refer to the same object they can see mutations to that object done by the other (this is why x changes in your example)

    SomeObject so = new SomeObject();
    so.x = 42;
    somefunction(so);
    Console.Write(so.x); // will print 42
    
    static void somefunction(SomeObject so) {
      so = new SomeObject();
      so.x = 13;
    }
    

    The ref modifier causes the parameter to be passed by reference instead of value. Effectively there is no copy of the reference, the so in both the original and calling function are the same reference. So resetting one resets the other

    SomeObject so = new SomeObject();
    so.x = 42;
    somefunction(ref so);
    Console.Write(so.x); // will print 13
    
    static void somefunction(ref SomeObject so) {
      so = new SomeObject();
      so.x = 13;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First this is not a duplicate questions. I've looked through some similar problem and
This is not a duplicate post. I've looked through similar questions on SO but
I've looked for similar questions with no success. I have this piece of code:
I think this should be simple. I have looked at similar questions here but
I read a few other articles on here that looked similar, but didn't quite
I've read similar questions here, but am still not clear on a couple of
I see there are some similar questions to this, but none solve my issue.
I've looked at similar questions here but I haven't found one particular to my
I have looked for similar questions, but could find none other than the difference
I've looked through the similar questions and not yet found an answer. I will

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.