C#:system.array Simple to use

Source: Internet
Author: User

Array 1.1 in 1.c# defines the array:

1.1.1 Definition uninitialized: Data type [] Array name = new data type [number of elements];
1.1.2 Definition and initialization: data type [] Array name = new data type []{array1,array2};

1.2 System.Array Class
C # provides an out-of-the-box class named System.Array that allows you to perform most operations on arrays using the properties and methods provided by this class.

1.2.1 System.Array object creation: The Array class is an abstract class and cannot be created using the following method
     
array array=new Array ();
         
the CreateInstance () method is provided in C # to create an array. For example:
   
Array Array=createinstance ((typeof (String), ten));
           
The statement creates an array with the name array, type string, and a length of ten.
          
The typeof is used to get the System.Type object of the type, and the type instance can represent classes, value types, arrays, interfaces, enumerators, and so on.

Common properties and methods of 1.2.2 arrays

1) Length Displays the total number of all dimensions in the array.

Array array = Array.CreateInstance (typeof (String), 10);

int length = array. length;//calculating the number of elements in an array

Console.WriteLine ("Array length ..." +length);

Results display: Array length ... 10

         2) BinarySearch Use the binary search method to search for a value in a one-dimensional sorted array.
         
Span style= "font-size:18px" >            This method is a static method: Array.BinarySearch method (Array, Object)  
             
             int arrayindex = Array.BinarySearch (Array, "name9");
     
            if value is found, the index of the specified value in the specified array.

If value is not found and value is less than one or more elements in the array,

is a negative number that is a bitwise complement of the index of the first element that is greater than value.

If value is not found and value is greater than any element in the array,

is a negative number, which is the bitwise complement of (the last element's index plus 1).
         
you need to sort the array before using this method.

3) Clear sets the set of primary colors in the array to 0 or Null
         
void Array.clear (Array, Index, Length);
             
4) Copy copies the set of elements in the array starting at the specified source index and pastes it into another array starting at the specified target index.

array.copy (Sourcearray, Destinationarray, length)

From the first element, copy a series of elements from the array, pasting them into another array (starting with the first element).

The length is specified as a 32-bit integer.

Array.copy (array, Int32, array, Int32, Int32)

Copies a series of elements from an Array, starting at the specified source index,

Paste them into another Array (starting at the specified target index). The length and index are specified as 32-bit integers.

5) CopyTo copies all the elements of an array into another array.

Copy all, starting with the specified index subscript, note that this is a non-static execution method.
Array. CopyTo (dest,0);

Sample code snippet for Copy and CopyTo methods.

............................................

     //creates an array of arrays and initializesArray array = array.createinstance (typeof(string),Ten); intLength =Array.            Length;  for(inti =0; i < length; i++)
{Array. SetValue ("name"+i,i); } //creates an array of dest and copies from array arraysArray dest = array.createinstance (typeof(string),5); //Index starting at 0, length 5Array.copy (Array, dest,5); //index starting at 1, length 5Array.copy (Array, dest,1,5); //copy all, starting with the specified index subscript, note that this is a non-static execution method. Array. CopyTo (dest,0);//Note that the target array is consistent with the number of source numbers. //result Output for(intj =0; J < Dest. length;j++) {Console.WriteLine ("dest"+j+"="+Dest. GetValue (j)); }

............................................

6) CreateInstance Initializes a new instance of the array class

Array array=array.createinstance (typeof (String), 10);


7) GetValue () returns the value of the specified element in the given array, which can be indicated by the specified index.

Array.getvalue (int index); Index starting from 0

8) SetValue () sets the specified element in the array to the specified value

Array.setvalue (Object value,int index);//index starting from 0

9) IndexOf Returns the position index of the first occurrence of a given value in a one-dimensional array

int Index=array.indexof (Array, "name1");//index starting from 0 ...

LastIndexOf returns the position index of the last occurrence of the given value in a one-dimensional array.

int Index=array.lastindexof (Array, "name1");//index starting from 0 ...

One) reverse the order of the elements in a single-dimensional array
Array.reverse (array);

Sort the elements in an array
Array.Sort (array);

The directly defined array differs from the array.createinstance ()

the directly defined array supports only the few properties and methods described above. Operations such as sorting, reversing, etc. are not supported. Therefore, simply storing the data element without performing an operation can use a directly defined array, and to operate on the array elements, create arrays with array.createinstance ().

C#:system.array Simple to use

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.