Basic knowledge of C #

Source: Internet
Author: User

1.string and StringBuilder

The string is immutable, a reference type inherits from an object (value type inherits from ValueType), and each time a string is stitched, it is actually constructing a new object on the managed heap. In this way, the repeated concatenation of the string will produce a large number of garbage string, automatically collected by GC, this time GC will be frequently recycled garbage string, affecting performance, so it is usually recommended to use StringBuilder to do string stitching,

So what's the point of string concatenation?

Here we have to talk about the concept of string pooling, when we create a string, we go to the string pool to find (hash table), if not found will create a new string and save to the string pool, when the created string has already existed in the string pool, it will be referenced. This allows the created string to be shared, saving space. Because in the program, we will use a large number of strings, if we each occurrence of the string in memory space to open a space to store, it will produce a lot of the same string in memory, it will also cause garbage collection frequent execution, affecting performance. So string concatenation is suitable for a small number of splicing, for a large number of splicing, it is recommended to use StringBuilder.

The following is a simple process of string concatenation and StringBuilder stitching.

string concatenation process: String a= ' a '; A+=b;

1. Open enough temporary storage space to secure enough storage for strings A and B.

2. Copy A to the beginning of the staging area.

3. Copy B to the end of the temporary storage area.

4. Release the old memory for a.

5. Assign a new memory to a.

6. Copy the temporary storage space string to the newly opened memory in 5 and point a reference to the new memory.

StringBuilder splicing Process: StringBuilder is a chain storage structure, the constructor initializes the size of the StringBuilder instance, the maximum capacity that can be stored, the current string. When the newly added string is greater than the current StringBuilder remaining space, StringBuilder will open a new space (size = original size), the new string fills the remaining memory space, the remaining string into the newly opened memory space. Quote a picture on the web.

The essential difference between the 2 is that string in the concatenation of strings is to create a new object to save the stitched string, and StringBuilder is the original StringBuilder object to open up new memory space, is to manipulate the original object rather than a string to create a new object.

2. Must the value type be on the stack?

We all know that the value type is stored on the thread stack, but only the value type is stored on the stack as a temporary variable, as a member variable, in the heap.

1. An internal variable of a reference type, even if it is a value type, is stored on the heap by the instance of the reference type, and the value type variable inside the method is not initialized and placed on the stack when the method is executed.

2. For an array of value types, because the array is a reference type. So the value types inside the array are also on the heap.

3. Closure, LAMDA expression. As follows:

Action<int> act = a =>{Console.WriteLine (a);};

C # into Il adds a static helper class, and local variables within closures become member variables of the helper class, so local variables of this value type are also assigned to the heap.

Closure traps: Refer to Poplar under the Bodhi tree: http://www.cnblogs.com/yjmyzz/archive/2009/03/13/1410924.html

usingSystem;usingSystem.Collections.Generic;namespaceconsoletest{classProgram {Static voidMain (string[] args) {List<Action> ls =NewList<action>();  for(inti =0; I <Ten; i++) {ls. Add (()=Console.WriteLine (i)); }            foreach(Action actioninchls)            {action ();        } System.Console.Read (); }           }  }

Result: 10 rows of exactly the same "10" were output (probably not by code writer's "intent", output 0 to 9)

See the link is actually compiled generated a class, the class has a variable I and the method of printing I, and then create an action delegate collection to assign the method, and finally iterate through the collection execution, at the time of execution I have been added to 10. Therefore, an internal variable is needed to avoid this trap.

3. How do I implement a hash table?

             

  

    

Basic knowledge of C #

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.