Immutablelist lines?

1) what is the advantage of immutability of strings? In c#, operations on strings return a new object. Further, the note says that this greatly affects performance. Then why the hash table rows are there? What are the pros, what cons?

2) Write a program that works with strings (many times per second). And strings are used not only to display text, but also for encoding numbers, Boolean values, etc. are Planning to rely on "brain" of the language in this question. But what method of storing, processing, and conversion of strings is the most optimal in this case?

3) Write C# and JavaScript. What methods embedded in these languages, it would be best to use? In Sharpe now use StringBuilder.
June 10th 19 at 16:33
4 answers
June 10th 19 at 16:35
1. Immutability of strings is used in comparison operations - compares only pointers. Still have questions save space when re-using, but not sure there is a lot to win.
2. For the active string is just used StringBuilder. in fact, they represent the allocated array is large in size and which operates with the line. During manipulation, a new array is not created - everything is in place, if not exceeded the allocated size. If the buffer is not enough - is the new, larger size. Current size of the buffer can be found from the Capacity property, it can directly ask large enough.
> 1. Immutability of strings is used in comparison operations - compares only pointers.

What is it like? Suppose we have received a string from the user, and the string from the database. And it is necessary to compare. The contents of these rows are the same, but are two different object. Comparing pointers does not work. - wayne76 commented on June 10th 19 at 16:38
I understand that in the RAM is loaded only one row. - Alize_Moen commented on June 10th 19 at 16:41
The language is so constituted that if you get a string that is already used in the program, you will assign the same pointer. That is, when reading from the database, if the string matches the user string, it will point to the same place. - halle commented on June 10th 19 at 16:44
Eeee... that is, every time we get a new string value is present, Palestrina (well, OK, it is possible to consider and compare the hashes, but then they also need to be stored somewhere) comparison with ALL string values currently available? And all this for what if(!) sometime later(!!), we need to compare two string values you can do this quickly? Ie any operation changing the string has complexity dependent on the number of existing string values? Plus the overhead of maintaining this table, the cleanup of debris, optimize indexes... In General, it is a DBMS in memory. It really is in C#? - wayne76 commented on June 10th 19 at 16:47
I made a mistake. What I described is called the Intern and can be used by programmers through the use of String functions.Intern. In this case the strings once in memory. A pairwise comparison is not happening - considered and compares hashes of strings. - halle commented on June 10th 19 at 16:50
June 10th 19 at 16:37
For starters, you need to understand that a string is essentially an array of characters. The length of the array change is not accepted. It certainly can be done, but the operation changes the size of the array is very resource-intensive. The array is data that is written to memory sequentially. When you try to change the size of the array, there is no guarantee that after the last element in the array is available memory, therefore have to copy the array in free space. In fact, for this reason, the size of the array do not change.

In order to produce any operations over the line, you need to change an array of characters.
Therefore, you need to make this very expensive operation, and copy the entire array. Why, then, this inconvenience is built into the sharp?

Why not use the signs of rupture? After all, if the chance that the next site is free, it's silly to copy the entire array when it can be supplemented. And if not, then copy it to the end+new characters in a new memory location, replace the old values to the pointer of the continuation of the array. - wayne76 commented on June 10th 19 at 16:40
It generates a lot of problems with performance evaluation. So for example, now have free the station everything happened quickly, after a second it slowly and you'll get the programmer to understand what's the problem? This is a very strange bug or just a feature of the runtime? Much better when you can turn on the Profiler and see what's really going on, and with this realization it is simply not possible. - Alize_Moen commented on June 10th 19 at 16:43
June 10th 19 at 16:39
1) Any comparison operations and sorting lines are much faster. Frequently used Dictionary dictionaries can be obtained very fast. And most importantly - in line will dotnet thread-safe
2)/3) do Not premature optimization, standard methods more than enough for the concatenation of strings of template strings ($""), StringBuilder, or a stream, if the volume is very large. String for encoding "numbers" is something unhealthy in architecture.
Any data type is a string, otherwise where did the value if it was not printed on the keyboard? - wayne76 commented on June 10th 19 at 16:42
That's the only "philosophy" is not necessary. I pointed to a particular school - if the program uses numeric data in memory, they should be kept just numbers. For the case of "printed on the keyboard" - the parseInt/isNaN/isFinite and further work with numbers. - Alize_Moen commented on June 10th 19 at 16:45
so every letter is also stored in memory as a number. - halle commented on June 10th 19 at 16:48
:-) Good luck in eating cacti. Decided to help out of kindness, but not to the horse feed. - wayne76 commented on June 10th 19 at 16:51
June 10th 19 at 16:41
If you can take, to allocate any chunk of memory to fit your needs, and then take and expand on it, without selecting a new parcel with the right size, this way you will be able to solve your problem. Just do it in C#, there is everything necessary for working memory. But can you handle it to avoid any errors and whether this solution is faster banal copy one memory area to another?

Find more questions by tags JavaScriptC#