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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:08:30+00:00 2026-05-28T08:08:30+00:00

Given, public class SomeClass { public string SomeName{get;} public List<string> RelatedNames{get;} } public class

  • 0

Given,

public class SomeClass {
     public string SomeName{get;}
     public List<string> RelatedNames{get;}
}

public class Program{
     public void Main(){
          var someClassInstance = new SomeClass(){ SomeName = "A", RelatedNames = new List<string>(1){ "a" }};
          // So, now someClassInstance have been allocated some memory in heap = 1 string object and a list with 1 string object.

          // Since SomeClass is mutable, it could be modified as below
          someClassInstance.SomeName = "Now This is much more than a name";
          someClassInstance.RelatedNames = someClassInstance.RelatedNames.AddRange(new List<string>(100} { "N","o","w".....});

         //Now what happens inside heap?
         //1.someClassInstance.SomeName will move it's pointer to another string inside heap
         //2.someClassInstance.RealtedNames will move it's pointer to another List<>(101) inside heap.
         //Is it correct? Then where is 'mutability' ?
     }
}

As mentioned in the comments above, “AFAIK” on modifying a mutable object the internal pointers of that object will just point to another memory location inside heap. If that is correct, then does that mean that all objects inside heap (reference type) are immutable?

Thanks for your interest.

  • 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-28T08:08:31+00:00Added an answer on May 28, 2026 at 8:08 am

    Where’s mutability? Right there:

    someClassInstance.SomeName = "Now This is much more than a name";
    someClassInstance.RelatedNames = new List<string>(100} { "N","o","w".....};
    

    You just mutated the object pointed to by someClassInstance.

    Also, your example is a bit contrived. Strings are indeed immutable, but Lists are not, so you could have done this:

    someClassInstance.RelatedNames.Add("HELLO!");
    

    And then you just mutated the object pointed to by someClassInstance.RelatedNames.


    EDIT: I see you changed your question. Well, then:

    1. someClassInstance.SomeName will move it’s pointer to another string inside heap
    2. someClassInstance.RealtedNames will move it’s pointer to another List<>(101) inside heap.

    1 is true because String was designed to be immutable. That’s why there’s the StringBuilder class in case you need a mutable string.

    2 is false, because that’s not how List is implemented. Perhaps that’s where your confusion comes from. Still, when you invoke AddRange, someClassInstance.RelatedNames will still point to the same instance, but that instance’s internal state will have changed (most likely, its backing array will have been changed to point to a different array object, and its count would now be 101). In fact, a reference cannot magically change based on the operations that are invoked to the object it refers to.

    And none of that changes the fact that someClassInstance‘s internal state was mutated anyway.

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

Sidebar

Related Questions

Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
Given the following class definitions: public class BaseClass { public string SomeProp1 { get;
Given the following object: public class Product { string Name {get;} int Quantity {get;}
given the following class ... public class Category { public string Name {get;set;} public
Good Morning! Given: public class FooClass { public void FooMethod() { using (var myEntity
Given the following class public class Foo { public int FooId { get; set;
Given a simple class: public class Person { public string FirstName; public string LastName;
Given this definition: [UniqueId(Ident)] public class MyClass : MyBase { public int Ident{ get;
I have a class like this: public class SomeClass { private const string sessionKey
Given interface public interface IHaveError { string ErrorMessage { get; set; } } Extension

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.