array of custom types
Using System; Namespace ConsoleApplication4 {class Program {static void Main (string[] args) { person[] mypersons = new person[2]; Mypersons[0] = new Person ("Lilian", "Chen"); MYPERSONS[1] = new Person ("1", "2"); Console.WriteLine (Mypersons[0]. ToString ()); Console.WriteLine (Mypersons[1]. ToString ()); Use an array initializer for a custom type person[] persons = new person[] {new Person ("A", "B"), New Person ("C", "D")}; Console.WriteLine (Persons[1]. ToString ()); Console.ReadLine (); }} public class Person {public person () {} public person (string f Irstname, String lastName) {this. FirstName = FirstName; LastName = LastName; } public string FirstName {get; Set } public string LastName {get; Set } public override string ToString () {return String.Format (' {0} {1} ', FirstName, last Name); } } }
Note: If the elements in the array are reference types, you must allocate memory for each array element.
Mypersons is a variable stored on the stack that references an array of person elements stored on the managed heap. Each item in the array refers to a person object.
Create an array
Declaring an array with [] is a notation using the array class in C #, and a new class derived from the abstract base class array is created in the background. This allows you to use the methods and properties defined by the array class for each C # array.
The array class is an abstract class, so you cannot use constructors to create arrays. However, in addition to using C # syntax to create an array instance, you can use the static method CreateInstance () to create an array (you can use this static method if you do not know the type of the element beforehand).
Using System; Namespace ConsoleApplication5 { class program { static void Main (string[] args) { // The first parameter of the CreateInstance method is the type of the element, the second parameter is the size of the array intarray = Array.CreateInstance (typeof (int), 5); for (int i = 0; i < intarray.length; i++) { //Use the SetValue () method to set the value Intarray.setvalue (i, I); } for (int i = 0; i < intarray.length; i++) { //Use the GetValue () method to read the value Console.WriteLine (Intarray.getvalue ( i)); Console.ReadLine (); } Cast an array that has already been created into an array declared as int[] int[] intArray1 = (int[]) Intarray; Console.WriteLine (Intarray1.length);}}}
Use the CreateInstance () method to create a multidimensional array and an array that is not based on 0:
Using System; Namespace ConsoleApplication4 {class Program {static void Main (string[] args) { Int[] lengths = {2, 3}; Int[] Lowerbounds = {1, 10}; Array racers = array.createinstance (typeof (person), lengths, lowerbounds); Racers. SetValue (New Person ("A", "B"), 1, 10); Racers. SetValue (New person ("C", "D"), 1, 11); Racers. SetValue (New person ("E", "F"), 1, 12); Racers. SetValue (New Person ("G", "H"), 2, 10); Racers. SetValue (New person ("I", "J"), 2, 11); Racers. SetValue (New Person ("K", "L"), 2, 12); person[,] racers1 = (person[,]) racers; Person first = racers1[1, 10]; }} public class Person {public person () {} public person (string f Irstname, String lastName) {this. FirstName = FirstName; LastName = LastName; } public string FirstName {get; Set } public string LastName {get; Set } public override string ToString () {return String.Format (' {0} {1} ', FirstName, last Name); } } }
Copying an array
1) If the element of the array is a value type, all values are copied
Int[] IntArray1 = {1, 2}; If delete "(int[])" will have Error "cannot implicitly convert type ' object ' to ' int[] '" int[] IntArray2 = (int[]) intarray1.cl One
If the array contains reference types, the elements are not copied, but only references are copied
If you modify the properties of an element in Beatlesclone, the corresponding object in the Beatles is changed.
Person[] beatles = {New Person ("John", "Lennon"), New Person ("Paul", "McCartney")}; Person[] Beatlesclone = (person[]) Beatles. Clone ();