Basic _ C # Array

Source: Internet
Author: User
C # Array

An array is a data structure that contains several variables of the same type. The array uses the type declaration: Type [] arrayname;

The following example creates a one-dimensional, multi-dimensional, and staggered array:

Array Overview

An array has the following attributes:

  • Arrays can be one-dimensional, multi-dimensional, or staggered.

  • The default value of the numeric array element is set to zero, and the default value of the referenced element is set to null.

  • An interleaved array is an array. Therefore, its element is of the reference type and its initialization is null. The dimension and size of the elements in the staggered array can be different.

  • The index of an array starts from zero: The index of an array with n elements ranges from 0 to n-1.

  • Array elements can be of any type, including arrays.

  • The array type is a reference type derived from the abstract base type array. Because this type implements ienumerable and ienumerable, you can use foreach Iteration for all arrays in C.

In C #, arrays are actually objects.Not just the addressable contiguous memory areas like C and C ++.

The system. array class provides many other useful methods and attributes for sorting, searching, and copying arrays. (Mscorlib. system. array object view)

Method Name Description Heavy Load
Array. Length Returns the length of an array.  
Array. Rank Display the dimension of the array  
Array. getupperbound (INT dimension) Obtain the upper limit of the specified dimension of system. array. Dimension: the dimension of array starting from scratch.  
Getlowerbound (INT dimension) Lower limit  
Object getvalue (INT index1, int index2) Return Index Value Multiple
Setvalue (object value, int index)    
Indexof <t> (T [] array, T value) Search for the specified T type object and return the index of the first matching item in the entire array. <T> The equals comparison method of the object must be reloaded. Multiple
Lastindexof <t> (T [] array, T value)

Search for the specified T type object and return the index of the last matching item in the entire array. Multiple
Sort <t> (T [] array) Use the system. icomparable <t> generic interface of each element of array to sort the elements in the entire array. <T> the icomparable interface must be implemented.

Multiple
Foreach <t> (T [] array, system. Action <t> action)   Multiple
More in object Viewer: mscorlib. system. ARRA    

Use foreach for Arrays(Multidimensional support)

 

Int [] numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0 };
Foreach (int I in numbers)
{
System. Console. writeline (I );
}

 

Passing arrays as parameters

Method definition for calling array parameters

Void printarray (INT [] ARR)
{
// Code
}

Transfer
You can pass the initialized one-dimensional array to the method. For example:
Printarray (thearray );

You can also initialize and pass a new array in one step. For example:
Void printarray (New int [] {1, 3, 5, 7, 9 })

When using ref and out to pass an array, the ref must be initialized, And the out can be not initialized.

Arraylist and list Array

The two are complex arrays and reference the system. Collections namespace

Arraylist or List objects are complex arrays. The arraylist class and list generic class provide some functions provided by most system. Collections classes but not provided by the array class. For example:

  • Array capacity is fixed, while arraylist or list capacityAutomatically scalable as needed. If the value of the capacity attribute is changed, memory reallocation and element replication can be performed automatically.

  • Arraylist and list provide addition, insertion, or removal.A range Element. In array, you can only get or set the value of one element at a time.

  • Using synchronized, you can easily create a synchronization version of arraylist or list. Array leaves the synchronization task to the user.

  • Arraylist and listReturns read-only and fixed-size packages to a set.Array is not provided.

On the other hand, array provides some flexibility required by arraylist and list. For example:

  • You can set the lower limit of the array, butThe minimum value is always zero..

  • Array can have multiple dimensions, while arraylist or listAlways only one-dimensional.

  • Array of a specific type (excluding objects) has better performance than arraylist. This is because the elements of arraylist belong to the object type. Therefore, the boxing and unboxing operations are usually generated when the value type is stored or retrieved. However, when no re-allocation is required (that is, the initial capacity is very close to the maximum capacity of the list), the list performance is very similar to the array of the same type.

You can use arraylist or list for arrays in most cases. They are easier to use and generally have similar performance with arrays of the same type.

Array is located inSystemIn the namespace; arraylist is located inSystem. CollectionsIn the namespace; List is located inSystem. Collections. GenericNamespace.

 

 

Class testarraysclass
{
Static void main ()
{
// Declare a single-dimen1_array
Int [] array1 = new int [5];

// Declare and set array element values
Int [] array2 = new int [] {1, 3, 5, 7, 9 };

// Alternative syntax
Int [] array3 = {1, 2, 3, 4, 5, 6 };

// Declare a two dimen1_array
Int [,] multidimensionalarray1 = new int [2, 3];

// Declare and set array element values
Int [,] multidimensionalarray2 = {1, 2, 3}, {4, 5, 6 }};

// Declare a jarged Array
Int [] [] jaggedarray = new int [6] [];

// Set the values of the first array in the jarged Array Structure
Jargedarray [0] = new int [4] {1, 2, 3, 4 };
}
}

 

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.