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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:23:29+00:00 2026-05-16T03:23:29+00:00

Why does the original string remain unchanged in example 1 and the original instance

  • 0

Why does the original string remain unchanged in example 1 and the original instance not get nulled in example 2, when the string property of an object instance is changed in example 3?

    class A { public string property = "class a"; }

    static void Main(string[] args)
    {

        //Example1
        string var = "something";
        string[] array = new string[] { var };
        array[0] += " again";

        // var is unchanged, contents in array slot zero are changed

        //Example2
        A instance = new A();
        object[] array2 = new object[] { instance };
        array2[0] = null;

        // instance is also unchanged, contents in array slot zero are now null

        //Example3
        A anotherInstance = new A();
        object[] array3 = new object[] { anotherInstance };
        (array3[0] as A).property = " else";

        // the *propery* of anotherInstance changes as expected

        return;
    } 
  • 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-16T03:23:29+00:00Added an answer on May 16, 2026 at 3:23 am

    When you assign a value in an array, you create a copy of the original value. You’re not creating a copy of the string itself, but a copy of the value of the “var” variable – which is a string reference. After that point of time, there’s no relationship between the variable and the array element – they happen to have the same value immediately afterwards, but they’re independent. Changing the array element to make it refer to a different object doesn’t change the value of the variable, or vice versa.

    In particular, this line:

    array[0] += " again";
    

    does not change the contents of the existing string. Instead, it creates a new string and assigns that reference to array[0].

    Now in your “Example3” you aren’t changing the value of the array element itself – you’re changing the contents of the object that both anotherInstance and array3[0] refer to. It’s like two people knowing about the same house – if one person paints the door red, the other person will see the red door too.

    This all has very little to do with arrays – you’d see the same effects with individual variables too. See my article on reference and value types for further information.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is way late, but have you considered using TraceSource?… May 16, 2026 at 11:44 am
  • Editorial Team
    Editorial Team added an answer when i use the above code it opens a new… May 16, 2026 at 11:44 am
  • Editorial Team
    Editorial Team added an answer You wrote: That one would want an item cached forever… May 16, 2026 at 11:44 am

Trending Tags

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

Top Members

Related Questions

My .net application does some heavy string loading/manipulation and unfortunately the memory consumption keeps
I have some problem with replace string in Ruby. My Original string : What
how does realloc know the size of original data? void *realloc(void *ptr, size_t size);
How does the Class-path in the MANIFEST.MF file determine the relative location? 1) Say
This is probably a question that is more about object alloc/retain/release, but I'll use
Hopefully I'll get some help here. Basic setup is this: My application swaps out
I'm not sure if this is even possible, but here goes: I have a
I have a dataset with multiple tables. One table in particular has one record
When I use this code to rotate the image, the destination image size remains
I have an Activity with an AutoComplete box on it. Whenever the text changes,

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.