C # Basic Grammar review-working with arrays and collections

Source: Internet
Author: User

What is an array?

An array is an unordered sequence of elements, and all elements in an array have the same type.

Declaring an array variable:

Int[] A; the size of the array is not part of the Declaration;

To create an instance of an array:

A=new Int[3];

Initialize the array variable:

Int[] A=new int[2]{1,2};

Int[] a={1,2};

Time[] schedule={new time, New Time (5,30)};

To create an implicitly-typed array:

var name=new[]{new {name= "John", age=44},new {name= "Diana", age=45}};

To iterate over an array:

You can use a for statement, or a foreach statement, to define the type as var when you do not know the type of the array.

To copy an array:

int pins={9,3,7,2};

Int[] Copy=new int[pins. Length];

Pins.copyto (copy,0);

Use System.Array copy (source, copy to array, length of array);

Using multidimensional arrays:

int[,] items=new int[4,6];

What's the collection class:

The elements in the collection class are of type object, and the values are boxed directly when they are stored;

ArrayList Collection class:

There is an add (), remove () insert () method;

Queue Collection Class:

There are two methods of Enqueque and dequeue;

Stack Collection class:

There are two types of push and pop;

Hashtable Collection class:

Hashtable a =new hashtable ();

A["Z"]=23;

a["W"]=24;

Froeach (dictionaryentry element in ages)

{

String Name= (String) Element.key;

int age= (int) element.value;//unboxing for forced type conversions

}

SortedList and Hashtable use the same method;

C # Basic Grammar review-working with arrays and collections

Related Article

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.