Latest web development tutorials

C # Array class

C # arrays C # arrays

Array class is all arrays in C # base class, which is defined in the System namespace. Array class provides various properties and methods for arrays.

Property Array class

The following table lists some of the most commonly used class Array properties:

序号属性 & 描述
1IsFixedSize
获取一个值,该值指示数组是否带有固定大小。
2IsReadOnly
获取一个值,该值指示数组是否只读。
3Length
获取一个 32 位整数,该值表示所有维度的数组中的元素总数。
4LongLength
获取一个 64 位整数,该值表示所有维度的数组中的元素总数。
5Rank
获取数组的秩(维度)。

For a complete list of the properties of the Array class, see the Microsoft C # documents.

Array class methods

The following table lists some of the most commonly used class Array method:

序号方法 & 描述
1Clear
根据元素的类型,设置数组中某个范围的元素为零、为 false 或者为 null。
2Copy(Array, Array, Int32)
从数组的第一个元素开始复制某个范围的元素到另一个数组的第一个元素位置。长度由一个 32 位整数指定。
3CopyTo(Array, Int32)
从当前的一维数组中复制所有的元素到一个指定的一维数组的指定索引位置。索引由一个 32 位整数指定。
4GetLength
获取一个 32 位整数,该值表示指定维度的数组中的元素总数。
5GetLongLength
获取一个 64 位整数,该值表示指定维度的数组中的元素总数。
6GetLowerBound
获取数组中指定维度的下界。
7GetType
获取当前实例的类型。从对象(Object)继承。
8GetUpperBound
获取数组中指定维度的上界。
9GetValue(Int32)
获取一维数组中指定位置的值。索引由一个 32 位整数指定。
10IndexOf(Array, Object)
搜索指定的对象,返回整个一维数组中第一次出现的索引。
11Reverse(Array)
逆转整个一维数组中元素的顺序。
12SetValue(Object, Int32)
给一维数组中指定位置的元素设置值。索引由一个 32 位整数指定。
13Sort(Array)
使用数组的每个元素的 IComparable 实现来排序整个一维数组中的元素。
14ToString
返回一个表示当前对象的字符串。从对象(Object)继承。

For a complete list of the Array class methods, see the Microsoft C # documents.

Examples

The following program demonstrates the use of the methods of the Array class:

using System;
namespace ArrayApplication
{
    class MyArray
    {
        
        static void Main (string [] args)
        {
            int [] list = {34, 72, 13, 44, 25, 30, 10};
            int [] temp = list;

            Console.Write ( "the original array:");
            foreach (int i in list)
            {
                Console.Write (+ "");
            }
            Console.WriteLine ();
           
            // Reversal array Array.Reverse (temp);
            Console.Write ( "reverse the array:");
            foreach (int i in temp)
            {
                Console.Write (+ "");
            }
            Console.WriteLine ();
            
            // Sort the array Array.Sort (list);
            Console.Write ( "sorted array:");
            foreach (int i in list)
            {
                Console.Write (+ "");
            }
            Console.WriteLine ();

           Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Original array: 34721344253010
Reversal array: 10302544137234
Sort array: 10132530344472

C # arrays C # arrays