What is the difference between string and stringbuilder? Why keep string? Is it just because it is traditional?

Source: Internet
Author: User

I know the difference between string and stringbuilder,
Every time a string is modified, a new string is created. Deleting the old string will affect the efficiency, while stringbuilder operates at the end of the string, which is faster than string,

In small databases, string and stringbuilder have no major impact, but in large data volumes, stringbuilder is much faster than string,
So why can't we get rid of the string in the. net Framework? Is it because of the traditional inheritance of other languages?

Thank you! Answer 1



------ Other answers (2 points )---------
For the first time, we considered that string is a value type. stringbuilder is a reference type. Each time we use it, we need to instantiate an object. This may be the difference between the two, but I am not clear about the specifics. wait for the explanation of the prawns ....
------ Other answers (2 points )---------
Stringbuilder has more powerful functions, which means its underlying implementation is more complex. Some simple functions use string, of course, are more concise and even efficient.

Like sorting algorithms, if there are only five pieces of data, can you use Bubble sorting or quick sorting?
------ Other answers (2 points )---------
Stringbuilder? I have never used it. Learn it.
------ Other answers (2 points )---------
People-oriented
------ Other answers (1 point )---------
String is the basic type. How can I remove it? stringbuilder is used when strings are frequently operated
------ Other answers (2 points )---------
Use stringbuilder to frequently modify strings. Use string in other places.
------ Other answers (2 points )---------
Reference the reply of sageking2 on the second floor:
Stringbuilder has more powerful functions, which means its underlying implementation is more complex. Some simple functions use string, of course, are more concise and even efficient.

Like sorting algorithms, if there are only five pieces of data, can you use Bubble sorting or quick sorting?
This guy is brilliant.
------ Other answers (2 points )---------
It has its own application scenarios.
....
------ Other answers (2 points )---------
You are talking about why you need a match with a lighter.
------ Other answers (2 points )---------
String is the basic type.

Without a string, how do you assign values to StringBuilder and other operations?

------ Other answers (2 points )---------
In. NET, String is an unchangeable object. Once a String object is created and assigned a value to it, it cannot be changed, that is, you cannot change the value of a String. String is a reference type. This means that if we create many String objects with the same value, the pointing address in the memory should be the same. That is to say, when we create String object a, its value is "1234 ", when we create another string object B with a value of "1234", it does not allocate a memory space, but directly points to the address of a in the memory.

The efficiency of String-type connection operations is quite low. Each connection operation creates a new object in the memory, it occupies a large amount of memory space. This leads to the StringBuilder object. The StringBuilder object is modified on the original string during the string connection operation, improving the performance. We may know that when connection operations are frequent, StringBuilder objects are used.

We can see that the connection validity rate of strings is very low, but StringBuilder is not used in all situations. When we connect a few strings, we can use String, however, StringBuilder must be used for a large number or frequent string connection operations.
The C # String object cannot be changed. Every time you use a method in the System. String class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the String repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use the System. Text. StringBuilder class. For example, when many strings are connected together in a loop, using the StringBuilder class can improve the performance.


------ Other answers (1 point )---------
Reference the reply from yanzhiyong on the 11 th floor:
That is to say, when we create String object a, its value is "1234 ", when we create another string object B with a value of "1234", it does not allocate a memory space, but directly points to the address of a in the memory.


Is this sentence true? Well worth your attention
According to what you said, When I generate object B whose memory is "1234" of object, the memory content will not be sorted by the value size. Isn't it necessary for me to cycle all the memory once before I can find the "1234" address to generate B?

Is that what you want to say?
C # code
string a = "1234";string b = a;

In this way, the memory will not be re-allocated ~~!

------ Other answers (1 point )---------
Reference the reply from yanzhiyong on the 11 th floor:
In. NET, String is an unchangeable object. Once a String object is created and assigned a value to it, it cannot be changed, that is, you cannot change the value of a String. String is a reference type. This means that if we create many String objects with the same value, the pointing address in the memory should be the same. That is to say, when we create String object a, its value is "1234 ", when we create another string object B with a value of "1234", it does not allocate a memory space, but directly points to the address of a in the memory.

The efficiency of String-type connection operations is quite low. Each connection operation creates a new object in the memory, it occupies a large amount of memory space. This leads to the StringBuilder object. The StringBuilder object is modified on the original string during the string connection operation, improving the performance. We may know that when connection operations are frequent, StringBuilder objects are used.

We can see that the connection validity rate of strings is very low, but StringBuilder is not used in all situations. When we connect a few strings, we can use String, however, StringBuilder must be used for a large number or frequent string connection operations.
The C # String object cannot be changed. Every time you use a method in the System. String class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the String repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use the System. Text. StringBuilder class. For example, when many strings are connected together in a loop, using the StringBuilder class can improve the performance.





Brother on the 11th floor, the explanation is correct.
When we create String object a with a value of "1234", when we create another string object B with a value of "1234", it will not allocate a memory space, instead, it directly points to the address of a in the memory.

A and B point to different addresses. Although a and B have the same value "1234", they are not the same object and they point to different addresses.
String type data, which is essentially an array of the Char [] Type

------ Other answers (1 point )---------
I think the question of this post is not why the string is retained, but why the stringbuilder is retained.

A string is a basic type and cannot be canceled. Basically, all objects must use a string. You have developers reference System for each object. the Text namespace generates the stringbuilder object, which is absolutely crazy!

However, how to extend the string to the same functions as stringbuilder without occupying more resources when generating objects, and retaining the performance advantages of stringbuilder when connecting a large number of strings? This may be the key to why string does not replace stringbuilder.

The internal structure of stringbuilder is unknown ~~
------ Other answers (1 point )---------
Learning


The reply content is too short!


------ Other answers (1 point )---------
C # code
 string a = "1234";            string b = "1234";            Console.WriteLine(a==b);            Console.WriteLine(a.Equals(b));            Console.Read();


Both are

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.