. NET Foundation Step by Step [Array, collection, exception capture]

Source: Internet
Author: User

Array, collection, exception capture

Array :

Store multiple variables of the same type at once.

One-dimensional arrays:

Grammar:

Array type [] array name =new array type [array length];

Syntax for declaring an array:

A. Data type [] Array name = new data type [2]{1,2};

B. Data type [] Array name = new data type [array size];

C. Data type [] Array name = {data, data, data, data};

Once the length of the array is fixed, it can no longer be changed.

Elements in an array can be accessed through an index: array name [index position]

Case:

Multidimensional arrays: Values for multiple linear arrays

Two-dimensional: int [,] arr = new int[3,5];

Three-dimensional: int [,,] arr = new int[2,4,5];

Multidimensional arrays use GetLength to get the length, the dimension is also starting from 0

Case:

Jagged arrays: arrays in arrays.

Declaration syntax: int [] arr = new Int[2][];//arr contains two elements

int [0] = new int[2];//The first element is a two-integer array

int [1] = new int[3];//The second element is an array of three integers

A jagged array has better performance than a multidimensional array.

Array Arrays:

Declaration syntax: Array arr = array.createinstance (typeof (int), 3);//type int, length 3

Collection:

In this article only to describe the landlord in the project of several commonly used collection classes;

[PS: Want to see a large collection of. NET, see http://www.kuqin.com/shuoit/20150331/345474.html]

ArrayList:

List :

Hashtable :

Hashtable with the Arraylist The difference:

Hashtable Key value mapping, simply say the key corresponding to the value, we recognize each value from the name, key so-called name, we look for the value by name, and Arraylist set, linear structure to store the data, set stored content values, And we are numbering each value with an index, and we use the index to traverse the process;

Arraylist Orderly, but Hashtable disorder;

Arraylist can add and remove elements at specific locations, and Hashtable can only be added sequentially.

Arraylist Index of 0, Hashtable object (Custom)

Dictionary :

Differences between arrays and collections:

1. The array is fixed size and cannot be scaled. Although System.Array.Resize this generic method resets the array size, the method is to recreate the new set size array, using the element initialization of the old array. Then the previous array is discarded! And the set is a variable length

2. Array to declare the type of the element, the element type of the collection class is object.

3. The array can be read and writable and cannot declare only the reading group. The collection class can provide the ReadOnly method to use the collection in a read-only manner.

4. Arrays have integer subscripts to access specific elements, but many times such subscripts are not very useful. The collection is also a list of data but does not use subscript access. Many times a collection has a custom subscript type that does not support subscript access for queues and stacks at all!

5. The array can have dimensions, and the collection does not.

Exception capture:

We often have a variety of anomalies in the program, if you want your program to become stronger.

You should use Try-catch frequently for exception trapping in your code.

Grammar:

Try

{

Code that may appear to be abnormal;

}

//try there can be no other code between the and catch

Catch

{

Code to execute after an exception occurs;

}

Execution procedure: If the code in the try does not have an exception, the code in the catch will not execute.

If there is an exception to the code in the try, then there are 100 lines behind the code that is not going to execute the exception.

Instead, jump directly into the catch to execute the code

try{

Throw exception

program code;

}catch (Exception e) {//captures and handles exceptions. If there are multiple catch blocks, the parent class (Exception) must be in the back

Exception handling code;

Console.WriteLine (e.message);//display text that describes the error condition

Console.WriteLine (E.source);//Displays the name of the application or object that caused the exception to occur

Console.WriteLine (e.stacktrace);//provides the details of the method called in the stack, and the method that was first called recently.

Console.WriteLine (e.innerexception);//provides access to internal exceptions.

}

finally{

Final processing

}

OK, this article is here, and another case to provide only, there is no running results, please handle it yourself. Hope to bring help to beginners, but also hope that the great God can bring us, take us to install force, take us to fly ...

Finally, a small ad: QQ group: . NET Step by Step screen number:590170361 (dabigatran Note: Blog Park to see)

. NET Foundation Step by Step [Array, collection, exception capture]

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.