. NET basic step by step curtain [array, set, exception capture],. net Array

Source: Internet
Author: User

. NET basic step by step curtain [array, set, exception capture],. net Array

Array, set, exception capture

Array:

Multiple variables of the same type are stored at a time.

One-dimensional array:

Syntax:

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

Syntax for declaring Arrays:

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 };

* ** Once the length of the array is fixed, it cannot be changed.

You can access the elements in the array through indexes: array name [index location]

Case:

 

 

Multi-dimensional array: Values of Multiple Linear Arrays

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

3D: int [,] arr = new int [2, 4, 5];

Use GetLength to obtain the length of a multi-dimensional array. The dimension starts from 0.

Case:

 

Staggered array: array in the array.

Declaration Syntax: int [] [] arr = new int [2] []; // arr contains two elements.

Int [0] = new int [2]; // The first element is an array of two integers.

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

Compared with multi-dimensional arrays, staggered arrays provide better performance.

 

Array:

Declaration Syntax: Array arr = Array. CreateInstance (typeof (int), 3); // type: int, Length: 3

 

Set:

This article only describes several collection classes that are commonly used in projects;

[PS: For details about how to view. NET integrated large inventory check, see http://www.kuqin.com/shuoit/20150331/345474.html]

ArrayList:

 

List:

 

Hashtable:

 

HashtableAndArraylistDifferences:

HashtableKey-value ing. in simple words, the corresponding value of the key is assigned a name for each value. The so-called name of the key is used to find the value.ArraylistData is stored in a set and in a linear structure, and all values are stored in the set. Each value is numbered by an index. During the traversal process, we use an index;

ArraylistOrder, andHashtableUnordered;

ArraylistYou can add or delete elements at specific locations.HashtableYou can only add them sequentially.

ArraylistIndex is 0,HashtableObject (custom)

 

Dictionary:

 

Differences between arrays and collections:

1. arrays are fixed in size and cannot be scaled. Although the generic method System. Array. Resize can reset the Array size, this method re-creates an Array with the new size and initializes it with elements of the old Array. Then the previous array will be discarded! The set is variable-length.

 

2. the array must declare the element type. The element type of the Collection class is object.

 

3. the read-only array cannot be declared for readable and writable arrays. The Collection class can provide the ReadOnly method to use the set in read-only mode.

 

4. the array must have an integer subscript to access specific elements. However, such subscript is not very useful in many cases. The set is also a data list, but it does not use subscript access. Most of the time there is a set of custom subscript types, which does not support subscript access for queues and stacks!

 

5. the array can have dimensions, but the set does not.

 

Exception Capture:

We often encounter various exceptions in the program. If you want your program to become stronger.

In your code, try-catch should be often used for exception capture.

 

Syntax:

Try

{

Abnormal code may occur;

}

// TryThere cannot be any other code between it and catch.

Catch

{

Code to be executed when an exception occurs;

}

 

Execution Process: If no exception occurs in the code in try, the code in catch will not be executed.

If there is an exception in the code in try, it is afraid that the code in this line will not be executed in the next one hundred lines,

Instead, it directly jumps to catch to execute the code.

 

Try {

// Throw an exception

// Program code;

} Catch (Exception e) {// catch and handle exceptions. If multiple catch blocks exist, the parent class (Exception) must be

// Exception Handling Code;

Console. WriteLine (e. Message); // displays the text describing the error status

Console. WriteLine (e. Source); // display the name of the application or object that causes an exception

Console. WriteLine (e. StackTrace); // provides detailed information about the methods called in the stack, and first calls the method recently.

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

 

}

Finally {

// Final processing

}

 

 

 

Well, this article is here, and the case provided in this article is only, there is no running result, please handle it yourself. I hope it will help beginners. I also hope that the great gods will take us with us, pack us with force, and fly us with us...

Finally, make a small advertisement: QQ group:. NET Step by stepGroup Number:590170361(Add group remarks: what you see in the blog Park)

 

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.