c # basic types of data

(A) string

      (1) String type is immutable, is the class of a reference. If a large need to modify the string, the StringBuilder can be used, which contains Append (), AppendFormat (), Insert (), Remove (), Replace () method and the like. The difference is that

  The method modifies the data StringBuilder itself, rather than simply returns a new string.

    (2) null shows a variable is set to no, and the referenced class can only be assigned properties, indicating that no variable values ​​different from "" which represents an empty string.

    (3) implicit type of local variables var. But best not to use in the case of the data type known.

(Ii) the classification of the class

      Divided into classes of value and reference, except that the value of the type of data replication is always a value, a reference type of data is always referenced replication.

   (2.1) value type, the variable reference position value is stored in the actual location in memory. Therefore, the value of a variable is assigned to the second variable creates a memory copy of the original value of the variable in the location of the new variable.

   (2.2) the value of the referenced class is stored in reference data storage locations. Therefore, when running the program, from the first reading position memory variables, and then "jump" to the memory location containing the data. Class of a reference point is called the heap memory area (heap).

               Unlike the reference type as in claim value type memory copy data is created, the copy of a reference instance of the class is more efficient than copying large value type instance.

  Conversion between (iii) Data Type

       May cause small or big to throw an exception of any conversion will need to perform display conversion, on the contrary it belongs to the implicit transformation.

     (3.1) Explicit Conversion

              For example, long converted to a variable of type int types of variables will cause the loss of accuracy, this time need to display conversion.

      (3.2) implicit conversion

            For example, int transition to long, will not lead to loss of precision, it is implicitly converted.

      (3.3) Transformation without the use of type conversion operator

         Not defined from a string to a numeric type conversion, it is necessary to use System.Convert, Parse (), a method in the TryParse (), allowing he converted to numerical values ​​corresponding to the type of a string.

        

            string text = "97E-31";

            float s = float.Parse(text);
 Double NUM;
             String input = " 34.5 " ;
             // and the Parse () as compared to the control if the input is not given, the return value is false. 
            IF ( Double .TryParse (INPUT, OUT NUM)) 
            { 
                Double value = NUM; 
            } 
            the else 
            { 


            } 

wherein int cou = default (int); retrieves default value of int.

 (D) array 

(4.1) + 1 = Number comma dimension (rank).

Examples and evaluation (4.2) of the array.

   

            // (1) of the array simultaneously assignment statement: 
            String [] = {languages " C # " , " the Java " }; 
            
            // again after assignment (2) declaration. 
            String [] La; 

            La = new new  String [] { " . 1 " , " 2 " };
             // (. 3) may be allocated arrays, but do not specify an initial value, but the "run" type of each element of the array initialization defaults . 
            String [] = Item new new  String [ . 9 ];

 

   (4.3) multi-dimensional arrays

    

  // (1) initialize a two-dimensional array --- 2 rows 3. 
            String [,] Array = new new  String [ 2 , . 3 ] {{ " . 1 " , " 2 " , " . 3 " }, { " . 4 " , " . 5 " , " . 6 " }}; 
           
            // initialize a three-dimensional array. cells is broken down into the following two String [,] of the array type, the size of each array is *. 3. 3 
            String [,,] cells; 

            cells = new new  String [ 2 , . 3 , . 3 ] {
            {{ "1", "2", "3" },{ "1", "2", "3" },{ "1", "2", "3" },},
            
            {{ "1", "2", "3" },{ "1", "2", "3" },{ "1", "2", "3" },}};

(4.4) staggered array

  Staggered array does not use a comma to identify a new dimension.

  int[][] cells = {new int[]{1,2,2,3},
                            new int[]{4,2},
                            new int[]{2,1,12,123,}};

Code above, int [] is the data type, then it [] declares a type int [] array,

Using (4.5) of the array

 Length indicates the total number of elements in the array, such as a three-dimensional array of size 2 * 3 * 3, the return value is 18, but in staggered array, it returns the number of elements of the array outside, so the above cells interleaving length is returned array 3. because arrays are staggered array of arrays thereof. Statistics only its number of elements (that is, the specific number is composed of an array), regardless of the various internal array contains a number of elements. 

To sort an array Note the use of BinarySearch method before, if not sorted in ascending return incorrect index.

Use Clear () method is not deleted when the elements of the array, but not the length of the array is set to zero, because the array size is fixed and can not be modified, he just each element in the array set to default array type . This also explains the time after calling this method the output array Console.WriteLine () method Why would key a blank line.

 

Get the length of the array in a particular dimension when using a GetLength () instance method, returns the length of a specified dimension, such cells.GetLength (1) = 3. Rank is the number of dimensions of the entire array, cells.Rank = 3;

By default, an array variable to another array variable only replicated reference to the array, but using the Clone () method returns a new copy of the array, change any of the members of this copy will not affect the members of the original array .

 

Guess you like

Origin www.cnblogs.com/anjingdian/p/11185318.html