1

I'm having problem in assigning a value to the specific index in the string after checking on the same index: Here below is my code:

"bits" is a string and "dirtybit" is an integer.

if (bits.ElementAt(dirtybit).Equals('1'))
      bits[dirtybit] = '0'; //shows red underlined error

Error:

Property or Indexer String.this[int] cannot be assigned to -- is only read

  1. Why can I not access the same index (value)?

  2. Is there any workaround?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
U. Ahmad
  • 76
  • 1
  • 11

1 Answers1

2

Strings are immutable in C#. You can not change them after you have created them.

You can use StringBuilder to create a new string.

From MSDN:

Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this.

Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74