471,350 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

writing to a strings index

I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only. Hmmm.
Why? I didn't declare it const or readonly?
Dec 6 '05 #1
6 1169
Strings are immutable. They cannot be changed.

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?

Dec 6 '05 #2
strings in .Net are immutable - i.e. you cannot change their content once
created; depending on your intended usage, you could use substring /
replace/ regex functions to create a new string based on yours, or you could
call .ToCharArray(), change the array, and then create a new string from the
array (one of the ctors accepts char[]).

Marc

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?

Dec 6 '05 #3
Steve,

Strings are treated as immutable. Once you create one, you can't change
the contents of it. You can use it as an input to create new strings.

In this case, you would have to create a new string, and concatenate
them together, like so:

test = (new String('t', 1)) + test.Substring(1);

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?

Dec 6 '05 #4
KH
Try the StringBuilder class
"Marc Gravell" wrote:
strings in .Net are immutable - i.e. you cannot change their content once
created; depending on your intended usage, you could use substring /
replace/ regex functions to create a new string based on yours, or you could
call .ToCharArray(), change the array, and then create a new string from the
array (one of the ctors accepts char[]).

Marc

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?


Dec 6 '05 #5
great, thanks for all the answers everyone! While I don't understant why
they implemented the string class like this, I'm sure there is a logical and
sound explanation. I have since used substring to accomplish what I need.

Thanks again!

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only. Hmmm. Why? I didn't declare it const or readonly?

Dec 6 '05 #6
Steve,

Mainly for performance reasons, as well as confusion... It just makes a
good number of things easier knowing it won't change.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
great, thanks for all the answers everyone! While I don't understant why
they implemented the string class like this, I'm sure there is a logical
and
sound explanation. I have since used substring to accomplish what I need.

Thanks again!

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.

Hmmm.
Why? I didn't declare it const or readonly?


Dec 6 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Michael B. Allen | last post: by
5 posts views Thread by Gary McCullough | last post: by
3 posts views Thread by Daniel Weinand | last post: by
9 posts views Thread by jerry.upstatenyguy | last post: by
9 posts views Thread by Omatase | last post: by
28 posts views Thread by hlubenow | last post: by
95 posts views Thread by hstagni | last post: by
17 posts views Thread by john | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.