---
title: "What do you mean by String objects are immutable?"  
description: "What do you mean by String objects are immutable?"  
author: "Anonymous User"  
published: 2014-10-13  
updated: 2020-09-15  
canonical: https://www.mindstick.com/interview/2224/what-do-you-mean-by-string-objects-are-immutable  
category: "asp.net"  
tags: ["asp.net", "c#"]  
reading_time: 2 minutes  

---

# What do you mean by String objects are immutable?

String objects are immutable as its state cannot be modified once created. Every time when we perform any operation like add, copy, replace, and case conversion or when we pass a string object as a parameter to a method a new object will be created.\
**Example:**String str = "ABC";\
str.Replace("A","X");\
Here Replace() method will not change data that "str" contains, instead a new string object is created to hold data "XBC" and the reference to this object is returned by Replace() method.\
So in order to point str to this object we need to write below line.str = str.Replace("A","X");Now the new object is assigned to the variable str. earlier object that was assigned to str will take care by garbage collector as this one is no longer in used.

## Answers

### Answer by Anonymous User

String objects are immutable as its state cannot be modified once created. Every time when we perform any operation like add, copy, replace, and case conversion or when we pass a string object as a parameter to a method a new object will be created.\
**Example:**String str = "ABC";\
str.Replace("A","X");\
Here Replace() method will not change data that "str" contains, instead a new string object is created to hold data "XBC" and the reference to this object is returned by Replace() method.\
So in order to point str to this object we need to write below line.str = str.Replace("A","X");Now the new object is assigned to the variable str. earlier object that was assigned to str will take care by garbage collector as this one is no longer in used.


---

Original Source: https://www.mindstick.com/interview/2224/what-do-you-mean-by-string-objects-are-immutable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
