c # string in properties with regard to the introduction and notes

Foreword

  string type in our actual project development in one of the most used type, string is a reference type that we all know, but in actual use, we will find that string and use our common reference types really do not like to see the following simple example:

        static  void the Main ( String [] args) 
        { 
            String hello = " My name IS Yuanhong " ; 
            Console.WriteLine ( String .Format ( " before machining: {0} " , hello)); 

            /// //, hello processing 
            MachHello (hello); 

            Console.WriteLine ( String .Format ( " processed value: {0} " , hello)); 

            Console.ReadLine (); 
        } 

        ///  <Summary> 
        /// for processing hello
         ///  </ Summary> 
        /// <param name="hello"></param>
        private static void MachHello(string hello)
        {
            hello = string.Format("{0},Nice to meet you",hello);
        }

   The actual results of the program run is: are the same before and after the value has not changed, according to the analysis to see if a reference type, which is also before the post-processing is not the same value, that is why? Is there a feeling something like a string value type it? Here we take a good look into the particularity of string.

 

Introduction to achieve internal string

  First of all: it is to be noted that the string is sealed modified, can not be inherited.

  Second: Through the above string underlying source code, we find that the underlying implementation is actually a char array to achieve, at initialization a string, the size of the system are initialized char array.

     string when it is created immobilized size, and is read-only, can not be modified

        In actual use, we change the string, in fact internally re-create a new string

        When the string passed as parameters, is actually a copy of the data transfer

  And finally: Now we look back at the results of the proceedings in the most open we can easily understand why it is there such a phenomenon

 

Note the use of string point

 1, to avoid the cost of additional storage space

  + Sign to avoid using string concatenation:

  See the following example of a:

String str1 = " Yuan " ; 
str1 = str1 + " Hong " ;
 /// / This creates two strings 3 String object 

String strNew = " Yuan " + " Hong " ; /// equivalent to strNew = "yuanhong", that is, in fact, after compiling this effect 
/// / will create a string object

 

  Look at an example:

 

/// / The following are two ways to achieve a return string 123 

/// / mode. 1 
String V11 = " . 1 " ;
 String V22 = V11 + " 2 " ;
 String V33 = V22 + " . 3 " ; 
retun V33; 
/// / 1 by way: the system creates a string object 5 

/// / 2 mode
 /// / 2 by way: the system will create four string object 
string V1 = " 1 " ;
 string V2 = " 2 " ;
 string V3 = " . 3 " ; 
retun V1 + V2 +V3; 

/// / from a line memory overhead for whom, obviously superior to Embodiment 1 Embodiment 2

 

  In the actual development, if the object of frequent string splicing operation, it is recommended to use StringBuilder

  Of course c # also has a way to simplify the string concatenation: String.Format, in fact, the principle is to achieve its internal StringBuilder

  2, as little packing

  Directly on the code examples:

 

String str1 = " yunghong " + 66 ; 

String str2 = " yunghong " + 66 .ToString (); 

/// / See the compiled code, found in the first line of code, there is a need for packing operation, packing operation is required unnecessary memory overhead, first: the need to value distribution memory type itself, but also to the same type of pointer and memory overhead distribution sector index

 

to sum up:

  In the actual development need to pay attention to the following points:

  1, avoid the boxing operation

  2, string concatenation avoid using the +

 

Guess you like

Origin www.cnblogs.com/xiaoXuZhi/p/XYH_String.html