C #: custom type array, array class

Source: Internet
Author: User

1. Custom Array

1 using system; 2 3 namespace consoleapplication4 4 {5 class program 6 {7 static void main (string [] ARGs) 8 {9 person [] mypersons = new person [2]; 10 mypersons [0] = new person ("Lilian", "Chen"); 11 mypersons [1] = new person ("1", "2"); 12 console. writeline (mypersons [0]. tostring (); 13 console. writeline (mypersons [1]. tostring (); 14 15 // use the array initiator 16 person [] persons = new person [] {new person ("A", "B") for the custom type "), new person ("C", "D")}; 17 console. writeline (persons [1]. tostring (); 18 console. readline (); 19 20} 21} 22 23 public class person24 {25 public person () 26 {} 27 28 public person (string firstname, string lastname) 29 {30 this. firstname = firstname; 31 lastname = lastname; 32} 33 34 Public String firstname {Get; set;} 35 Public String lastname {Get; set;} 36 Public override string tostring () 37 {38 return string. format ("{0} {1}", firstname, lastname); 39} 40} 41}

Note: If the elements in the array are of reference type, you must allocate memory for each array element.

Mypersons is a variable stored on the stack, which references the array of person elements stored on the managed stack. Each item in the array references a person object.

 

2. Create an array

Using [] to declare an array is the mark of the array class in C #. A new class derived from the abstract base class array will be created in the background. In this way, you can use the array class to define the methods and attributes for each C # array.

The array class is an abstract class, so you cannot use constructors to create arrays. In addition to using the C # syntax to create an array instance, you can also use the static createinstance () method to create an array (if you do not know the element type, you can use this static method ).

1 using system; 2 3 namespace consoleapplication5 4 {5 class program 6 {7 static void main (string [] ARGs) 8 {9 // The first parameter of the createinstance method is the element type, and the second parameter is the array size 10 array intarray = array. createinstance (typeof (INT), 5); 11 for (INT I = 0; I <intarray. length; I ++) 12 {13 // use the setvalue () method to set the value of 14 intarray. setvalue (I, I); 15} 16 17 for (INT I = 0; I <intarray. length; I ++) 18 {19 // use the getvalue () method to read the value 20 console. writeline (intarray. getvalue (I); 21 console. readline (); 22} 23 24 // forcibly convert the created array to an array declared as int [] 25 int [] intarray1 = (INT []) intarray; 26 console. writeline (intarray1.length); 27} 28} 29}

Use the createinstance () method to create multi-dimensional arrays and arrays not based on 0:

 1 using System; 2  3 namespace ConsoleApplication4 4 { 5     class Program 6     { 7         static void Main(string[] args) 8         {    9             int[] lengths = { 2, 3 };10             int[] lowerBounds = { 1, 10 };11             Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);12 13             racers.SetValue(new Person("a", "b"), 1, 10);14             racers.SetValue(new Person("c", "d"), 1, 11);15             racers.SetValue(new Person("e", "f"), 1, 12);16             racers.SetValue(new Person("g", "h"), 2, 10);17             racers.SetValue(new Person("i", "j"), 2, 11);18             racers.SetValue(new Person("k", "l"), 2, 12);19 20             Person[,] racers1 = (Person[,])racers;21             Person first = racers1[1, 10];22         }23     }24 25     public class Person26     {27         public Person()28         { }29 30         public Person(string firstName, string lastName)31         {32             this.FirstName = firstName;33             LastName = lastName;34         }35 36         public string FirstName { get; set; }37         public string LastName { get; set; }38         public override string ToString()39         {40             return String.Format("{0} {1}", FirstName, LastName);41         }42     }43 }

3. Copy the Array

  • If the array element is of the value type, all values are copied.
1 int [] intarray1 = {1, 2}; 2 3 // if you delete "(INT []) "There will be an error" cannot implicitly convert type 'object' to 'int [] '"4 int [] intarray2 = (INT []) intarray1.clone

 

  • If the array contains the reference type, only the reference is copied instead of the element.

If you modify the attribute of an element in beatlesclone, the corresponding object in Beatles is changed.

1             Person[] beatles = { new Person("John", "Lennon"), new Person("Paul", "McCartney") };2             Person[] beatlesClone = (Person[])beatles.Clone();

 

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.