So, basically I am practicing some algorithms. And I am trying to figure out why the following code is giving me an error when I try to set the value of number[i]? I know it is probably simple but I don’t ‘why’ it does not work.
public int SumOfRandomNumbersWithStrings(string randomness)
{
//Get the value of each index in the array
string number = "";
for (int i = 0; i < randomness.Length; i++)
{
number[i] = randomness[i];
}
//temporarily one until I finish the algorithm
return 1;
}
Because strings in C# are immutable.
Arrays of characters are mutable, though, so you can do this:
The most common way of building strings in C# is by using the
StringBuilderclass. It lets you change the content of the string by appending, removing, or replacing characters inside the string.