Logo
  • About
  • Portfolio
  • Developer Notes
  • Contact

C# Strings Immutable

C# with ASP.NET Core.

C# Strings Immutable
AHMED Abderrahmane
11/25/2025 12:00:00 AM
C# Strings Immutable

Is the string in C# actually immutable? 🤔 Let's see the truth!

People always say that
The string in hashtag#C kind is immutable (I mean we can't modify it)
When you come to try it yourself : 👇

string name = 'Omar' ;
name = 'Hany' ;
Console.WriteLine(name); -- hany

You say to yourself :
'How can he not be adjusted?! I actually changed the value and stayed Hany!' 😅

The truth is that you didn't edit the same string
You created a completely new object in memory with the value of 'Hany'
And the old object that was in Omar

And to prove this to you see the example in the picture
and you will find me doing name1 and name2

They were pointing to the same object in the first ('Omar')
When I changed name1, a new object was used in memory with the value 'Hany'
name2 is still pointing to the old 'Omar'

After we know that any modification of the string creates a new object:
The logical 👇 question
'Is it a best practice to use a string if it changes its value too much?'
Answer: No
Because each modification creates a new object, which consumes memory and reduces performance.

© 2025-26 Abderrahmane AHMED

  • About
  • Portfolio
  • Developer Notes
  • Contact
  • theme deafult color
  • LTR
  • RTL