C # Study Notes ---- Array

Source: Internet
Author: User

If you want to use multiple objects of the same type, you can use a set and an array.

 

Array is a reference type

 

Simple Array

Multi-dimensional array

int[,,] threedim = {    {{1,2},{3,4}},    {{5,6},{7,8}},    {{9,10},{11,12}}    };Console.WriteLine(threedim[0,1,1]);

 

Sawtooth Array

int[][] jagged = new int[3][];jagged[0] = new int[2]{1,2};jagged[1] = new int[6]{3,4,5,6,7,8};jagged[2] = new int[3]{9,10,11};

 

Array class

The array class is an abstract class, so you cannot use constructors to create arrays.

You can use the static createinstance () method to create an array.

 

Copying an array will implement the icloneable interface for the array. The clone () method defined by this interface creates a superficial copy of the array.

 

The array class uses the quicksort algorithm to sort elements in the array. The sort () method requires the implementation of the icomparable interface by elements in the array.

 

public enum PersonCompareType{    FirstName,    LastName}public class PersonComparer:IComparer<Person>{    private PersonCompareType compareType;        public PersonComparer(PersonCompareType compareType)    {        this.compareType = compareType;    }    public int Compare(Person x,Person y)    {        if(x == null) throw new ArgumentNullException("x");        if(y == null) throw new ArgumentNullException("y");        switch(compareType)        {            case PersonCompareType.FirstName:                return x.FirstName.CompareTo(y.FirstName);            case PersonCompareType.LastName:                return x.LastName.CompareTo(y.LastName);            default:                throw new ArgumentException(                    "unexpected compare type");        }    }}Array.Sort(persons,    new PersonComparer(PersonCompareType.FirstName));foreach(var p in persons){    Console.WriteLine(p);}

 

Arraysegment <t>

The arraysegment structure <t> indicates an array segment.

int[] ar1 = {1,4,5,11,13,18};int[] ar2 = {3,4,5,18,21,27,33};var segments = new ArraySegment<int> [2]{    new ArraySegment<int> (ar1,0,3),    new ArraySegment<int> (ar2,3,3)};

 

Enumeration

You can use enumeration in a foreach statement to iterate the elements in a set without having to know the number of elements in the set.

The foreach statement uses an enumerator.

 

An array or set of ieumerable interfaces with the geteumerator () method

The geteumerator () method returns an enumeration that implements the ieumerable interface.

 

The foreach statement uses the methods and attributes of the ienumerable interface to iterate all elements in the set.

 

C #'s foreach statement will not be resolved to the foreach statement in the Il code

The C # compiler converts the foreach statement to the methods and attributes of the ienumerable interface.

 

Yield statement

The yield Return Statement returns an element of the set and moves it to the next element.

Yield break stops Iteration

 

Tuples
Arrays merge objects of the same type, while tuples merge objects of different types.

Both arrays and element groups implement the istructuralequatable and istructuralcomparable interfaces.

C # Study Notes ---- Array

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.