{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/8700","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/8700/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/8700/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/8700/events","html_url":"https://github.com/dotnet/coreclr/issues/8700","id":196922127,"number":8700,"title":"On the immutability of System.String","user":{"login":"hadibrais","id":5206180,"avatar_url":"https://avatars.githubusercontent.com/u/5206180?v=3","gravatar_id":"","url":"https://api.github.com/users/hadibrais","html_url":"https://github.com/hadibrais","followers_url":"https://api.github.com/users/hadibrais/followers","following_url":"https://api.github.com/users/hadibrais/following{/other_user}","gists_url":"https://api.github.com/users/hadibrais/gists{/gist_id}","starred_url":"https://api.github.com/users/hadibrais/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadibrais/subscriptions","organizations_url":"https://api.github.com/users/hadibrais/orgs","repos_url":"https://api.github.com/users/hadibrais/repos","events_url":"https://api.github.com/users/hadibrais/events{/privacy}","received_events_url":"https://api.github.com/users/hadibrais/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-12-21T12:33:37Z","updated_at":"2016-12-21T13:18:00Z","closed_at":null,"body":"This is not exactly a bug report or a feature proposal, but I think that this is the most appropriate place to discuss the issue.\r\n\r\nI was wondering about the reason behind making the primitive `System.String` type immutable. By searching in the Internet,  I found [this](http://stackoverflow.com/questions/2365272/why-net-string-is-immutable) and [this](http://stackoverflow.com/questions/93091/why-cant-strings-be-mutable-in-java-and-net) StackOverfow posts in which the same question is asked. But, unfortunately, all answers (including the highly-upvoted and accepted ones) given there actually answer a different question that is \"What are the benefits of immutable strings?\" rather than the posed question \"Why is `System.String` immutable?\" This motivated me to look in the CLI specification, hoping to find an answer. Much to my surprise, the spec does not even say that `System.String` must be immutable. It just says in I.8.2.2 that `System.String` represents a Unicode string. It also doesn't specify that a string has to be preceded with a size field and has to be zero-terminated. I also looked in the C# spec which also only says that `System.String` represents a Unicode string.\r\n\r\nThis leads to the following questions:\r\n\r\n- I think that the reason behind making `System.String` immutable is for the sake of semantic consistency with other primitive types. That is, making characters to a string like bits to an integer. If we want to change an instance of `Int32`, we have to assign to it a new value. Similarly, if we want change an instance of `String`, we have to assign to it a new value (which is a reference to another string). This makes the set of primitive types elegant and simple. Am I right about this? (maybe someone from Microsoft can look in the original design document of `String`?).\r\n\r\n- Is String guaranteed to be immutable across CLI-conformant implementations or is it just an implementation detail? The same question for the size field and the terminating zero. Also the same questions for C#-conformant implementations.\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/268520790","html_url":"https://github.com/dotnet/coreclr/issues/8700#issuecomment-268520790","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/8700","id":268520790,"user":{"login":"hadibrais","id":5206180,"avatar_url":"https://avatars.githubusercontent.com/u/5206180?v=3","gravatar_id":"","url":"https://api.github.com/users/hadibrais","html_url":"https://github.com/hadibrais","followers_url":"https://api.github.com/users/hadibrais/followers","following_url":"https://api.github.com/users/hadibrais/following{/other_user}","gists_url":"https://api.github.com/users/hadibrais/gists{/gist_id}","starred_url":"https://api.github.com/users/hadibrais/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadibrais/subscriptions","organizations_url":"https://api.github.com/users/hadibrais/orgs","repos_url":"https://api.github.com/users/hadibrais/repos","events_url":"https://api.github.com/users/hadibrais/events{/privacy}","received_events_url":"https://api.github.com/users/hadibrais/received_events","type":"User","site_admin":false},"created_at":"2016-12-21T13:18:00Z","updated_at":"2016-12-21T13:18:00Z","body":"> Because it's likely more common to need an immutable string rather than a mutable one.\r\n\r\nIndeed. But this can justify a standard immutable string type, not a primitive one. Arguably, some integral primitive types are much less common than others, yet there are primitive.\r\n\r\n> An immutable string is simpler and thus more likely to be treated as a primitive type.\r\n\r\nYes. This is my proposed answer to the question.\r\n\r\n> As the elements are readonly you can return the same string reference for things (e.g. error messages, enum names, field names for serialization etc). If it was read/write then you need to copy the string before passing it to things as they may change it; so that would be extra overhead and allocations.\r\n\r\nYou are mentioning some of the benefits of immutable strings which do not answer the question as I explained earlier.\r\n"}}
{"actor":{"display_login":"hadibrais"}}