Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9276725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:55:15+00:00 2026-06-18T16:55:15+00:00

When I try to assign to individual characters in a string with s[i] =

  • 0

When I try to assign to individual characters in a string with s[i] = c I get a compiler error:

string s = "bcc";
s[0] = 'a'; // shows compile time error - indexer cannot be assigned - it's readonly

However this works:

s.ToCharArray()[0] = 'a';

and we can also completely assign the string to acc:

s = "acc"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:55:16+00:00Added an answer on June 18, 2026 at 4:55 pm

    This:

    s="acc"
    

    … is changing the value of the variable to refer to a different string. If this were allowed:

    s[0] = 'a';
    

    that wouldn’t be changing the value of the variable – it would have to change the contents of the string that s referred to. Strings are immutable in .NET, so that’s not allowed.

    It’s important to differentiate between changing the value of a variable and changing the data within the object it refers to.

    Changing the contents of s.ToCharArray() doesn’t do anything to either s or the string – it just mutates the newly-created array which is a copy of the data within the string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Because each time I try to assign a word to one it doesn't work
I've just got an error. When I try to assign an object like this:
during the prepareForSegue I try to assign a value to property and get the
When I try to assign a string to an array like this: CoverageACol[0,0] =
Why doesn't the compiler complain when I try to assign incorrect values to variable
I get a shmget: Invalid argument error while i try to execute this part
Every time I try to assign an include/require to a variable, it adds a
i try to assign date value which i get for JDateChooser (toedter component), to
when i try to assign values to dynamic array using function scanf, i'll get
First time using this technique, seems that regardless what attributes I try to assign

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.