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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:45:39+00:00 2026-05-15T01:45:39+00:00

Could you please explain the following behavior of C# Class. I expect the classResult

  • 0

Could you please explain the following behavior of C# Class. I expect the classResult as “Class Lijo”; but actual value is “Changed”.

We’re making a copy of the reference. Though the copy is pointing to the same address, the method receiving the argument cannot change original.

Still why the value gets changed ?

public partial class _Default : Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String nameString = "string Lijo";

        Person p = new Person();
        p.Name = "Class Lijo";

        Utilityclass.TestMethod(nameString, p);
        string classResult = p.Name;
        Response.Write(nameString + "....." + classResult);
    }
}

public class Utilityclass
{
    public static void TestMethod(String nameString, Person k)
    {
        nameString = "Changed";
        k.Name = "Changed";
    }
}

public class Person
{
    public string Name
    {
        get; set;
    }
}

Update: When I pass a String, it does not get actually changed.

  • 1 1 Answer
  • 2 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-15T01:45:39+00:00Added an answer on May 15, 2026 at 1:45 am

    The briefest answer is: read my article on parameter passing which goes into this in a fair amount of detail.

    The slightly longer answer is to compare these two methods, both of which use value parameters:

    public void ChangeMe(string x)
    {
        x = "changed";
    }
    
    public void ChangeMe(Person x)
    {
        x.Name = "changed";
    }
    

    In the first case, you are changing the value of the parameter. That is completely isolated from the original argument. You can’t change the content of the string itself, because strings are immutable.

    In the second case, you are changing the contents of the object which the parameter’s value refers to. That’s not changing the value of the parameter itself – it will be the same reference. To give a real world example, if someone delivers something to your house that changes the contents of your house, but it doesn’t change your house’s address.

    If you changed the second method to this:

    public void ChangeMe(Person x)
    {
        x = new Person("Fred");
    }
    

    then the caller wouldn’t see any change. This is closer to what you’re doing with a string – you’re making the parameter refer to a different object, rather than changing the contents of the existing object.

    Now, when you use a ref parameter, the variable used by the caller as the argument is “aliased” with the parameter – so if you change the value of the parameter, that changes the value of the argument as well. So if we change the last method like this:

    public void ChangeMe(ref Person x)
    {
        x = new Person("Fred");
    }
    

    then:

    Person y = new Person("Eric");
    ChangeMe(ref y);
    Console.WriteLine(y.Name);
    

    this will print out “Fred”.

    The key concept to understand is that the value of a variable is never an object – it’s either a value type value or a reference. If an object’s data is changed, that change will be visible through other references. Once you understand that copying a reference isn’t the same as copying an object, the rest falls into place reasonably easily.

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

Sidebar

Related Questions

Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Could someone please explain the difference between the following two lines of code: 1.
Could someone please explain to me why the following query is invalid? I'm running
Could someone please explain the Objective-C difference between myString and anotherString in the following
Could you please explain why the shell redirection doesn't work with System.Diagnostics.Process class? I
Could someone please explain the following compiler error to me: struct B { };
Could someone please explain why following program will output 4,6 instead of 4,4. MainProgram()
Could you please explain why the following code is not working as expected. The
could someone please explain why the following code is throwing an error? // JavaScript
Could someone please explain what is meant by the following ? You must define

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.