📜  C#数组类

📅  最后修改于: 2020-10-31 02:48:27             🧑  作者: Mango

C#数组类

C#提供了一个Array类来处理与数组相关的操作。它提供了用于创建,操作,搜索和排序数组元素的方法。此类用作.NET编程环境中所有数组的基类。

C#数组类签名

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Array : ICloneable, IList, ICollection, 
IEnumerable, IStructuralComparable, IStructuralEquatable

注意:在C#中,数组不是集合的一部分,而是被视为集合,因为它基于IList接口。

C#数组属性

Property Description
IsFixedSize It is used to get a value indicating whether the Array has a fixed size or not.
IsReadOnly It is used to check that the Array is read-only or not.
IsSynchronized It is used to check that access to the Array is synchronized or not.
Length It is used to get the total number of elements in all the dimensions of the Array.
LongLength It is used to get a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
Rank It is used to get the rank (number of dimensions) of the Array.
SyncRoot It is used to get an object that can be used to synchronize access to the Array.

C#数组方法

Method Description
AsReadOnly(T[]) It returns a read-only wrapper for the specified array.
BinarySearch(Array,Int32,Int32,Object) It is used to search a range of elements in a one-dimensional sorted array for a value.
BinarySearch(Array,Object) It is used to search an entire one-dimensional sorted array for a specific element.
Clear(Array,Int32,Int32) It is used to set a range of elements in an array to the default value.
Clone() It is used to create a shallow copy of the Array.
Copy(Array,Array,Int32) It is used to copy elements of an array into another array by specifying starting index.
CopyTo(Array,Int32) It copies all the elements of the current one-dimensional array to the specified one-dimensional array starting at the specified destination array index
CreateInstance(Type,Int32) It is used to create a one-dimensional Array of the specified Type and length.
Empty() It is used to return an empty array.
Finalize() It is used to free resources and perform cleanup operations.
Find(T[],Predicate) It is used to search for an element that matches the conditions defined by the specified predicate.
IndexOf(Array,Object) It is used to search for the specified object and returns the index of its first occurrence in a one-dimensional array.
Initialize() It is used to initialize every element of the value-type Array by calling the default constructor of the value type.
Reverse(Array) It is used to reverse the sequence of the elements in the entire one-dimensional Array.
Sort(Array) It is used to sort the elements in an entire one-dimensional Array.
ToString() It is used to return a string that represents the current object.

C#数组示例

using System;
namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating an array
            int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
            // Creating an empty array
            int[] arr2 = new int[6];
            // Displaying length of array
            Console.WriteLine("length of first array: "+arr.Length);
            // Sorting array
            Array.Sort(arr);
            Console.Write("First array elements: ");
            // Displaying sorted array
            PrintArray(arr);
            // Finding index of an array element
            Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));
            // Coping first array to empty array
            Array.Copy(arr, arr2, arr.Length);
            Console.Write("Second array elements: ");
            // Displaying second array
            PrintArray(arr2);
            Array.Reverse(arr);
            Console.Write("\nFirst Array elements in reverse order: ");
            PrintArray(arr);
        }
        // User defined method for iterating array elements
        static void PrintArray(int[] arr)
        {
            foreach (Object elem in arr)
            {
                Console.Write(elem+" ");
            }
        }
    }
}

输出:

length of first array: 6
First array elements: 0 5 7 8 9 25
Index position of 25 is 5
Second array elements: 0 5 7 8 9 25
First Array elements in reverse order: 25 9 8 7 5 0