C # difference between string and stringbuilder supplemented by two knowledge points and comparison between interfaces and abstract classes in the deep mechanism C # and their applicable scenarios

Source: Internet
Author: User

 

String StringBuilder

I am not going to sort out the chat records of myself in the coding migrant workers group.

 

Gu Weiwei (979605089) 11:16:25

Wei Zai? Let me take a look at my understanding of string and stringbuilder.

 

String is a constant and is unchangeable. The stringbuilder variable. You must carefully understand the variables and immutations here. The string s = "0"; s = "1" is used to indicate that the string is variable. I do not understand the concept of constants. There is a doll on the upstairs to give an example of which is the three strings and the one with a string. In fact, he understands it right, but he lacks knowledge in other places, so he is mistaken. Let's use the following example.

 

String s = "aa"; s + = "bb"; three aa bb aabb instances exist in the memory.

Stringbuilder ss = "aa"; ss. Append ("bb "). There are only two "bb" aabb "in the memory, and one" aa "is missing here, because stringbuilder is variable and" aa "is changed to" aabb "instead of allocating new space.

 

The landlord actually asked why stringbuilder is better than string, or why there is a string. Because stringbuilder is of the reference type, it requires a lot of space at the beginning of allocation, while string is of the value type. This is a bit of a comparison between struct and class. If not frequently spliced ,. The performance of string is obviously better than that of stringbuilder, so one person on the upstairs is right. If it is frequently spliced, use stringbuilder. If it is displayed, use string.

 

Wei wenyuan (3518499) 11:19:13

String is used to process a small number of characters.

StringBuilder is used to process a large number of characters.

Note: Processing refers to operations such as append, rather than mechanical operations.

 

Gu Weiwei (979605089) 11:19:39

That's right.

Wei wenyuan (3518499) 11:20:53

Not mechanized assignment

Str + = "OK ";

Str. append ("OK ");

All characters are accumulated. The performance of the current computer cannot be different if there are no more than one thousand characters in it.

If the number is more than one million, there should be a difference. stringBuilder is short in time.

 

Gu Weiwei (979605089) 11:22:21

Well. Yes. In the final analysis, it is because the string is immutable in the memory. If splicing is implemented, it is another open space, that is, the existence of three copies.

 

Wei wenyuan (3518499) 11:23:22

I feel immutable.

 

Gu Weiwei (979605089) 11:23:44

I still feel that stringbuilder is variable.

 

Wei wenyuan (3518499) 11:24:04

I understand it in C ++.

 

Gu Weiwei (979605089) 11:24:38

In C ++, you can understand the final char. That must be immutable.

 

Wei wenyuan (3518499) 11:24:42

To store Dongdong, you must first open up space, and when using StringBuilder, there is a limit on the allocation space. Therefore, when you use up the space, you can open up the memory space to continue.

 

Gu Weiwei (979605089) 11:25:41

Stringbuilder needs to allocate space. I feel that it is used to show space to put a char array. Of course, they are all empty. In this way, you can base the char array by assigning values to the position of the char array. In fact, the elements in the char array of stringbuilder can be operated. However, the char array in string cannot be operated. If you operate the string char array, you actually need to reassign a char array instead of directly operating the char on the chat array.

 

Wei wenyuan (3518499) 11:27:02

By the way, the difference is that the concat method called by String creates a new String object, while the append method of StringBuilder returns the application of the original object.

The String object cannot be changed. Every time you use System. when one of the methods in the String class or when performing operations (such as value assignment and concatenation), You must create a new String object in the memory, in this case, you need to allocate a new space for the new object. StringBuilder does not. 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.

 

Wei wenyuan (3518499) 11:27:46

Although the StringBuilder object is a dynamic object that allows you to expand the number of characters in its encapsulated string, you can specify a value for the maximum number of characters it can accommodate. This value is called the capacity of the object and should not be confused with the string length of the current StringBuilder object. For example, you can create a new instance of the StringBuilder class with the string "Hello" (Length: 5) and specify the maximum capacity of this object to 25. When you modify StringBuilder, it does not re-allocate space for itself until the capacity is reached. When the capacity is reached, a new space is automatically allocated and the capacity is doubled. You can use one of the overloaded constructors to specify the capacity of the StringBuilder class.

 

Gu Weiwei (979605089) 11:28:16

Well, I know that.

 

Chen Hao (461825637) 11:28:16

In the process of life variables, StringBuilder can allocate its own size. If the actual content exceeds the memory space, it will automatically double

 

Wei wenyuan (3518499) 11:29:25

I don't know whether to copy a string or use a tag to open up a new space when he doubles, and then use a logical connection.

However, the performance will be improved either way.

 

Gu Weiwei (979605089) 11:29:52

This is actually the idea of stack memory management. I understand. In fact, there is an algorithm used to control the frequent changes of the actual size of the array on the boundary of the original allocation size.

 

 

Interfaces and abstract classes:

The interface and abstract class are two completely different programming methods in c. Abstract classes are inheritance for implementation, while interfaces are inheritance for interfaces.

Abstract classes are used for abstraction.ShareTo modify or extend the abstract class of the derived class. The focus is on sharing some Members, reducing the amount of code and improving the code reuse rate. Therefore, when creating an abstract class model, we pay more attention to the number of classes we have available for sharing. Because of this motivation, the abstract class can contain everything that is included in the normal class, in order to maximize code reuse.

Interfaces are designed for interface programming. They are really object-oriented programming ideas. interfaces are actually based on the dependency reversal principle. Our attention lies in the external performance of things, instead of being entangled in internal implementations. Therefore, when abstracting the interface model, we are more focused on external conventions. We do not consider the details of the internal implementation, but only focus on the externalConventions. This ensures that the customer program only needs to understand the Implemented interfaces to understand the services provided by this module. In fact, the interface-oriented interface can also be interpreted as service-oriented programming. It is also because these motivations of the interface are applicable to scenarios that allow the interface to contain only declarations, no implementers, no member variables, but attributes. This is also because the interface members cannot be modified by access modifiers. In short, all implementation-oriented ideas cannot be applied in interfaces.

In fact, it shows the motivations of interfaces and abstract classes. interfaces are for external conventions, while abstract classes are for internal implementations. So some people will ridicule this way:

Interfaces are used to call external personnel. when creating a project, you only need to provide an interface for external calls. others don't need to worry about how you implement it, just like the slots on the motherboard. only the NIC slot is required externally. I do not know the specific implementation. abstract classes are used to call internal personnel. if a class has several Derived classes, you can define this class as an abstract class. the members in the Team can use different implementation methods to implement this abstract class according to the actual situation.

Of course, the above ridicule also reflects a deep understanding. I can see an abstract understanding on the forum, and I feel very good about it. I will use the interface to describe a set of methods. Abstract classes are used to describe a virtual object. So some people use is-a (abstract class) and like-a (Interface) to describe the relationship between them.

So many times, the trade-off between these two uses mainly depends on our motives, such as crows and sparrows. When we want to describe the virtual object of birds, we use abstract classes, but if we only focus on the method, we will choose the interface. Many times we will find that both interfaces and abstract classes can be implemented, but different trade-offs have already reflected our understanding of the problem.

In addition, because the interfaces and abstract classes have different motivations, they also have many differences. interfaces are a set of methods, and a class can accept method specifications from multiple places, just like a person can be managed by multiple organizations. Abstract classes are virtual objects. Therefore, a class can only inherit from one abstract class, just as a person has only one father.

 


 

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.