1. Array’s common attribute Length returns the number of elements. LongLength is used when the number of elements exceeds the range of int type. Rank returns the dimension of the array.
2. The Array class is an abstract class and cannot use the constructor to create an array.
But it can be done
view plaincopy to clipboardprint?
Array iArray = new int[] { 1, 2, 3, 4 };
for (int i = 0; i < iArray.Length; i++)
{
Console.WriteLine(iArray.GetValue(i));
}
Array iArray = new int[] { 1, 2, 3, 4 };
for (int i = 0; i < iArray.Length; i++)
{
Console.WriteLine(iArray.GetValue(i));
}
Or use the static method CreateInstance, especially when the array element type is not known in advance, you can define the array like this
The SetValue method is used to set, and the GetValue method is used to read.
view plaincopy to clipboardprint?
Array array = Array.CreateInstance(typeof(string), 3);
array.SetValue("Tom", 0);
array.SetValue("Jack", 1);
array.SetValue("Bill", 2);
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array.GetValue(i));
}
Array array = Array.CreateInstance(typeof(string), 3);
array.SetValue("Tom", 0);
array.SetValue("Jack", 1);
array.SetValue("Bill", 2);
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array.GetValue(i));
}
※This method can also be used to create multi-dimensional arrays
3. To copy an array, use the Clone() method.
If the array elements are of value type, all values will be copied,
If the array element is a reference type, copy the reference
※ If you need a deep copy of an array containing a reference type, you must iterate the array and create a new object
To illustrate the copying of reference types, see the following class
view plaincopy to clipboardprint?
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string FirstName, string LastName)
{
//When the parameter has the same name as a member of the class, use this to refer to the class member (personally it is recommended not to use it this way)
this.FirstName = FirstName;
this.LastName = LastName;
}
//Rewrite the ToString class
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
class Program
{
static void Main(string[] args)
{
Person[] persons1 = new Person[2];
persons1[0] = new Person("Tom", "lin");
persons1[1] = new Person("Jack", "Rader");
Person[] persons2 = persons1.Clone() as Person[];
persons2[0].FirstName = "Copy 2";
//Call to copy source array
Console.WriteLine(persons1[0].FirstName);
//The result is copy 2, not Tom. It can be seen that the Clone method only copies the reference.
Console.Read();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string FirstName, string LastName)
{
//When the parameter has the same name as a member of the class, use this to refer to the class member (personally it is recommended not to use it this way)
this.FirstName = FirstName;
this.LastName = LastName;
}
//Rewrite the ToString class
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
class Program
{
static void Main(string[] args)
{
Person[] persons1 = new Person[2];
persons1[0] = new Person("Tom", "lin");
persons1[1] = new Person("Jack", "Rader");
Person[] persons2 = persons1.Clone() as Person[];
persons2[0].FirstName = "Copy 2";
//Call to copy source array
Console.WriteLine(persons1[0].FirstName);
//The result is copy 2, not Tom. It can be seen that the Clone method only copies the reference.
Console.Read();
}
}
3. The Array class also implements bubble sorting of elements in the array, the Sort method.
For simple arrays, you can sort directly
Array.Sort(iArray); where iArray is an int array
-