C # basic knowledge Series 3 (class and struct, String and StringBuilder, equals and =)

Source: Internet
Author: User

This section mainly describes the similarities and differences between classes and struct, as well as the usage of String and StringBuilder, equals and =, in fact, we can see that the value type and reference type, stack, and packing operations in the previous section are used in many places. It is applied to practice slowly, so that the theory and practice can be combined.

Differences between classes and struct:

1. different keywords are class and struct.

2. Different types: reference type and value type (Storage: a heap and a stack ). Detailed visibility into the value type and reference type as well as heap and stack http://www.cnblogs.com/aehyok/p/3504449.html

3. the struct has no default constructor (can be added) or destructor. abstract, protected, and sealed cannot be used for modification.

4. After the Struct variable is used, the memory allocation will be automatically removed. The Class instance has a garbage collection mechanism to ensure memory recycling.

5. Inheritance. The structure cannot inherit from another structure or be inherited, but can inherit from the interface like the class.

6. fields can be declared in the struct, but the initial values cannot be given when fields are declared.

7. If we do not display the definition constructor in the object class, there will be an implicit constructor without parameters (After reloading the constructor, We need to display the declaration of the constructor without parameters ),

Constructor with no parameters is invisible in the struct.

8. constructors that can be displayed in the class with no parameters defined, and constructors that cannot be displayed in the struct.

9. struct can be New, and struct constructors must assign values to all fields. even a constructor without parameters assigns an initial value of 0 to the value type and a null value to the reference type.

Same:

1. All have attributes and Methods

2. The same as the class can inherit from the interface

The 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 performance.

 StringBuilder    MyStringBuilder    =        StringBuilder();   

By initializing variables using an overloaded constructor method, you can create a new instance of the StringBuilder class, as described in the following example.

  Set capacity and length 
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. The following code example specifies that the MyStringBuilder object can be expanded to a maximum of 25 white spaces.

StringBuilder    MyStringBuilder    =        StringBuilder(,    );   

In addition, you can use the read/write Capacity attribute to set the maximum length of an object. The following code uses the Capacity attribute to define the maximum length of an object.

MyStringBuilder.Capacity    =    ;   

The EnsureCapacity method can be used to check the current capacity of the StringBuilder. If the capacity is greater than the passed value, no changes are made. However, if the capacity is smaller than the passed value, the current capacity is changed to match the passed value.

You can also view or set the Length attribute. If you set the Length attribute to a value greater than the Capacity attribute, the Capacity attribute is automatically changed to the same value as the Length attribute. If you set the Length attribute to a value smaller than the string Length in the current StringBuilder object, the string is shortened.

Here is a blog article about the webmaster of the great God: Use string. Format need to pay attention to a performance problem http://www.cnblogs.com/dudu/archive/2012/05/29/string_format_stringbuilder.html

Which of the following is the high efficiency of StringBuilder, String. concat (), String + String? Http://q.cnblogs.com/q/36917/

For the value type, if the object value is equal, the equal operator (=) returns true; otherwise, false.

For reference types other than string, if two objects reference the same object, the = returns true. For the string type, = compares the string value.

= The operation compares whether the values of the two variables are equal.

The equals () method compares whether the content of the two objects is consistent, and equals is used to compare whether the reference type is a reference to the same object.

The value type is relatively simple. Here we mainly look at the reference type:

       Name { ;  Person(.Name =  Main( a =  ( [] { , , , ,  b =  ( [] { , , , , == g = h ====  Person(=  Person(===  Person(===

Result output:

Because the value type is stored in the memory stack (later referred to as the stack), the reference type variable is only the address of the reference type variable in the stack, and it is stored in the heap.

= The operation compares whether the values of two variables are equal. For a referenced variable, it indicates whether the addresses of the two variables stored in the heap are the same, that is, whether the stack content is the same.
The equals operation indicates whether the two variables are referenced to the same object, that is, whether the content in the heap is the same.

String is a special reference type. in C #, many methods (including the equals () method) of the string object are overloaded ), make the string object use the same value type.
Therefore, in the preceding example, the two comparisons of string a and string B are equal.

G. equals (h) uses the equals () method of sting, so it is equal (polymorphism ). If you modify strings a and B as follows:
String a = "aa ";
String B = "aa ";
Then, the two comparisons of g and h are equal. This is because the system does not allocate memory to string B, but points "aa" to B. Therefore, a and B point to the same string (the string is optimized by memory in this case ).

P1 and p2 are two different objects in the memory, so the addresses in the memory must be different, so p1 = p2 will return false, because p1 and p2 are references to different objects, p1.equals (p2) returns false.
For p3 and p4, p4 = p3, p3 assigns a reference to the object to p4. p3 and p4 are references to the same object, so true is returned for both comparisons.

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.