Use collections to organize related data

Source: Internet
Author: User

Collection Overview

The set is simply the upgraded version of the array. He can dynamically define and maintain the length of the collection (that is, the maximum number of elements in the collection)!

ArrayList

ArrayList are very similar to arrays. It's also called an array list, its capacity can be dynamically expanded as needed, and its indexes are redistributed and adjusted based on the expansion of the collection capacity. That is, the subscripts of the elements in the ArrayList collection are indeterminate and mutable.

The ArrayList class belongs to the System.Collections namespace, which contains interfaces and classes that define a collection of various objects such as lists, queues, bit arrays, hash tables, and dictionaries.

Syntax:
Using System.Collections; // Import Namespaces // defining ArrayList Objects ArrayList    ArrayList Collection name =new  ArrayList ("Length");
Common methods and properties of ArrayList
Property name Description
Count Gets the number of elements actually contained in the ArrayList
return value type Method name Description
Int Add (Object Value) Add an object to the end of the ArrayList
Void RemoveAt (int index) Removes the element at the specified index ArrayList
Void Remove (Object Value) Remove a specific object from the ArrayList
Void Clear () Remove all elements from the ArrayList
HashTable

In ArrayList we can access the elements in the collection by index, but it becomes very troublesome to find the position (index) of each element when the index of the elements within the collection changes frequently.

C # provides a data structure called Hashtable, often referred to as a hash table and also called him a "dictionary," which is the name of the dictionary because it is very similar to the dictionary, all through a word to find out more information about the word, A hashtable is a value that organizes data by key (key)

Common properties and methods of Hashtable
Property Description
Count Gets the number of key-value pairs contained in the Hashtable
Keys Gets the collection that contains the keys in the hastable
Values Get a collection that contains hastable

  return value Type method name                               description
void ad D (Object key,object Value)
remove (object  Key)
void clear () Clears all elements in Hashtable
Generics and generic collections

Generics and generic collections

The data stored through ArrayList and Hashtable is converted to the object type, which means that it can store different types of elements in a collection, and it is possible to raise the issue of forcing type conversion errors when traversing the collection. Generic collections are type-safe and define the types of elements in the collection, and the generic and generic collections are explained in detail below.

Generic type

Generics are a new feature in c#2.0, where generics introduce a concept: type parameters that reduce the risk of runtime coercion of type conversions or boxing unboxing operations by using type parameters (T), the most common application of which is to create collection classes, to maximize code reuse, to protect type safety, and to improve performance. You can constrain the types of elements in a collection class. A more typical generic set is list<t> and DICTIONARY<K,V>, which are explained in detail below,

Generic collection

In the System.Collections.Generic namespace, you define a de-multiple generic collection class that can replace the above ArrayList

The syntax for defining a List<t> collection is as follows:

List <T> Set name =new list<t> ();

T in <T> can constrain the element types in the collection, and the T table name collection manages the element types.

The difference between list<t> and ArrayList
    1. list<t> constraints on saved element types, ArrayList can save any type of element
    2. List<t> saving a value type element does not carry out a boxing unboxing operation and ArrayList
Generic collection dictionary<k,v>

Generic collection dictionary can replace the above Hashtable

Defines the syntax for a generic collection dictionary<k,v>

Dictionary<k,v> Collection name =new dictionary<k,v> ();

In "<K,V>" K represents the type of key in the collection, V represents the type of value, and their meaning and list<k,v> are the same

      1. dictionary<k,v> constraints on saved element types, Hashtablet can save any type of element
      2. Dictionary<k,v> saving a value type element does not carry out a boxing unboxing operation and Hashtablet
Generic class

The use of generics in collections is just one of the many applications of generics that have been used in classes, methods, etc.

The syntax for defining a generic class is as follows:

 Public class     Class name <T>{    //...}

T refers to a type parameter that represents a specific data type, which can be a value type or a reference type

Use collections to organize related data

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.