DotNET knowledge point Summary 4 (notes Integration), dotnet knowledge point

Source: Internet
Author: User

DotNET knowledge point Summary 4 (notes Integration), dotnet knowledge point
1. enumeration: essentially Class 2. IEnumerable Interface

As long as this interface is implemented, you can use foreach for traversal. The essence of a foreach loop is to call this interface to return an iterator, and call the MoveNext () method of the iterator to realize the loop.

Source code:

{IEnumerator GetEnumerator (); // returns an iterator} public interface IEnumerator {bool MoveNext (); object Current {get;} void Reset ();}

The above is not difficult to see:

The IEnumerable interface mainly includes the GetEnumerable method (get iterator object), The MoveNext method (check whether the next element of the loop exists), and The GetCurrent method (get the element of the current loop)

3. Basic concepts of collection

A collection is a container that can hold a bunch of things. Mainly dividedNon-generic setAndGeneric set

ArrayList-- What actually stores data is an Object [] array, and its generic type is List <T>

    HashTable --A set of non-generic key-value pairs, which corresponds to a Dictionary <TKey, TValue>

Differences and advantages of dynamic arrays and generic sets:

 

Dynamic arrays have no constraints on elements. You can add any dynamic array. The root cause is that it stores data in an array of the Object type.

A generic set is a set with element type constraints (there is a constraint on the elements added)

The generic set avoids the performance loss caused by packing and unpacking operations, and checks the parameter type when adding elements.

Common Methods

Add (), AddRange (), Insert (), InsertRange (), remove (), removeAt ()

Reverse () -- Reverse the element in the Set

4. Hash table internal mechanism:

HashTable, a set of key-value pairs, which is an array bucket []. There are three things: Key, value, and key.

Data is mainly stored in: private bucket [] buckets;

private struct bucket{    public object key;    public object val;    public int hash_coll;}

  Access operation:

    Storage principle:The subscript is calculated based on the hash value of the key. When we Add an element to HashTable, the subscript of the element stored in the HashTable array is calculated based on the hash value of the added Key (but because the hash value takes the length of the modulus group, so it will certainly not exceed the length of the current array ).

The Hashcode calculated by each object is not unique. Multiple objects may have the same HashCode. Solution:

A. hash once again

B. In the bucket mode, two objects with the same hashcode are loaded in a unified location.

C. When the new feature is added, the container array in HashTable is full, and it is resized twice as much as the array.

Principles:When we get an element from HashTable (based on the key), the subscript of the element to be retrieved is calculated based on the hash value of the key, compare the key values in the element with the hash values of the current key parameter, and compare whether the references of the two keys are consistent. If both conditions are met, the elements to be retrieved are determined.

5. Generic set

Reference namespace: System. Collections. Generic

List <T> -- T is the storage type.

Dictionary <K, V>

6. Performance Comparison Between List <T> and ArrayList

The ArrayList storage value type requires packing conjecture, while the storage reference type requires type conversion. The List <T> obviously does not require these additional overhead, and the high performance is obvious.

7. Use Time of try statement Block

Network Operations (sockets), file operations (IO-related), Database Operations, Division operations (when the divisor is 0), forced type conversion operations

8. Windows Form Program Files

The relationship between designer. cs design class and foreground class is partial)

9. Path class

ChangeExtension)

Combine -- Combine two paths into one path. [automatic processing of path separators]

GetDirectoryName -- get the path name of the file

GetExtension -- get the file extension

GetFileName -- get the file name part of the file path

GetFileNameWithoutExtension -- get the file name with the removed extension.

GetFullPath -- get the full path of the file and obtain the absolute path based on the relative path.

GetTempFileName -- get a unique temporary file name

GetTempPath -- get the path of the Temporary Folder

............

10. Operate folders and directories

Delete -- Delete the directory [recursive indicates whether to recursively Delete]

Exists -- determine whether a directory Exists

Move -- move

GetDirectories + get a subdirectory under the Directory

GetFiles -- get the file under a directory

............

11. Operating Files

File-operations on files and static classes. Copy and delete. Cut and so on

AppendAllText -- append the text contents to the file path [if the file does not exist, create it]

Exists -- determine whether the file path Exists

ReadAllLines -- read text files to a String Array

ReadAllText -- read text files to strings

WriteAllText -- save the text contents to the file path and overwrite the old content

Copy -- Copy a file [true indicates "Overwrite" when the file exists. If "true" is not added, an exception is reported when the file exists]

Create -- Create a file

Delete -- if the file does not exist, an error is returned.

DirectoryInfo -- a "class" of a folder to describe a folder object

GetParent -- get the parent directory of the Directory

FileInfo -- folder, used to describe a folder object

  Note:

    Use Assembly. GetExecutingAssembly (). Location to obtain the path of the current exe file. Do not use Directory. GetCurrentDirectory (). Because the current working Directory of the program is obtained, this path may change.

Modify the text encoding and use GetEncoding to obtain the encoding.

12. file stream

FileStream -- any type

StreamWriter -- string writing

StreamReader -- string read

13. essence of using statements

In essence, it is a try {......} Finally statement Block

All classes whose resources are to be released Using the Using keyword must implement the IDisposable interface.

Close executes Dispose () and recycles the object.

The Using statement block cannot catch the exception, because the Using only uses try ...... The finally operation does not have a catch block. Therefore, you must add try... to the using block ...... Catch Block to catch exceptions

14. serialization and deserialization

Serialization features

Binary formatter

Serialization:The binary formatter saves the fields and values in an object as text.

[Serializable]

BinaryFormatter Class Method

Serialize -- the object graph is serialized to stream.

Deserialize -- serialize an object from the stream. The returned value is the deserialized object.

Notes for serialization:

A. the serialization type must be marked as [Serializable].

B. the type of parent class must also be marked as [Serializable]. During reflection, a parameter must be "whether to find parent class Object"

C. The type of all members of this type must also be marked as [Serializable]

D. serialization only serializes the serial numbers of fields in the class (only some status information can be serialized)

Deserialization:The binary formatter first creates an identical object, and then sets the value of the field saved in "text" to the field. Use the reflection technology to create a new object based on the class name in the text (field, field value, Class Name) information, and set the value of a field to the new object.

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.