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

  • SEARCH
  • Home
  • 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 143743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:08:37+00:00 2026-05-11T08:08:37+00:00

Since strings are immutable in .NET, why are they copied for simple operations such

  • 0

Since strings are immutable in .NET, why are they copied for simple operations such as Substring or Split? For example, by keeping a char[] value, int start and int length, a substring could be created to simply point to an existing string, and we could save the overhead of copying the string for many simple operations. So I wonder, why was the decision chosen to copy strings for such operations?

For example, was this done to support the current implementation of StringBuilder? Or to avoid keeping a reference to a large char[] when only a few characters are required? Or any other reason you can think of? Can you suggest pros and cons for such design?

As mentioned by @cletus and supported by @Jon Skeet, this is more like asking why .NET strings were built differently from Java in this aspect.

  • 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. 2026-05-11T08:08:37+00:00Added an answer on May 11, 2026 at 8:08 am

    That’s basically the way that Java works. There are a few benefits of the .NET way, IMO:

    • Locality of reference – the data and the length are in the same place
    • Fewer dereferences – the data is at a fixed point within the string object itself; no need to dereference another char array
    • Lack of aliasing when you’ve got a single character substring of an originally-large string, as mentioned by Renaud.
    • You end up with fewer objects and variables. In the case of a .NET string (assuming no wasted buffer space), the total size (on x86) is approximately 20+2*n bytes. In Java you’ve got the size of the array (12 + 2*n) bytes and the string itself (24 bytes: object overhead, reference, start and count; it also caches the hash if it’s ever calculated it). So for an empty string, the .NET version takes about 20 bytes compared with Java’s 36. Of course that’s the worst case, and it’ll only be that ‘constant difference’ out – but if you use a lot of independent strings that could end up being significant. More for the garbage collector to look at, too.

    Of course, the benefits are in terms of requiring less space when the aliasing above doesn’t occur.

    In the end it will depend on your usage – the compiler and runtime can’t predict which usage pattern is more likely in your exact code.

    There may also be interop benefits of the current string representation, but I don’t know enough about that to say for sure.

    EDIT: I’m not sure why your question has received so many somewhat-hostile answers. It’s certainly not a ‘dumb’ way of representing a string, and it clearly works. Fears about data loss and complexity are pretty much just FUD in this case, I believe – the Java string implementation is simple and robust. I personally suspect that the .NET way of doing things is more efficient in most programs, and I suspect MS did research to check that, but there will certainly be situations where the ‘shared’ model works better.

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

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In you Global.asax.cs file, you will have the following route… May 11, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer What's happening here is that you're placing a pointer to… May 11, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer (I am the developer of Taste, which is now part… May 11, 2026 at 6:55 pm

Related Questions

I'm afraid that this is a very silly question, but I must be missing
In Ruby, methods which change the object have a bang on the end: string.downcase!
I am trying to create a utility method to perform mail merge-like functionality on
What is the easiest way to compare strings in Python, ignoring case? Of course

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.