C#_ Collections and generics

Source: Internet
Author: User

Collection

In practical programming, a set of data, such as stack operations, queue operations, or series data increases, deletes, changes, and checks, is often processed and calculated according to certain requirements. C # provides an array type for management, but one drawback is that the size of the array is fixed. We can manage it through the collection class. The data collection classes are located in the System.Collections namespace and are referenced when used.

Comparison of arrays and collections:

1, ArrayList class

ArrayList is a collection of dynamically expandable capacity.

New ArrayList ();

2, Hashtable class

Hash Table Hashtable is a data dictionary that stores data as a key/value.

New Hashtable ();

A key-value pair is an object type, using the

3. Stack class

This set of stacks operates on an element, called a stack set, in the "FIFO" or "LIFO" principle.

New Stack ();

4. Queue class
New Queue ();

Collection-Class generics

Why reference a collection class generic?

A non-generic collection that stores member objects as real types of type object, which can store any type of element, but increases the performance overhead of boxing and unpacking.

static void Main (string[] args) {  ArrayList array = new ArrayList ();  int i = ten;  Array. ADD (i);  I =array[0];//error here, because the collection stores the object type, requires (int) coercion type conversion}  

The method of restricting access to a particular data type avoids the tedious operation of the collection and deals specifically with some type.

1, List<t> class

Create a list (a list can store any type of data, and when you create a list object you first specify what type of list you want to store) (generics)

New List<t> ();

T is an element type, and adding elements to the collection needs to be constrained by T.

list<int> scorelist =Newlist<int>(); List<int> scorelist =Newlist<int> () {1,2,3} List<int> scorelist =Newlist<string> () {" One"," Both"}  varScorelist =Newlist<int> ();
Declaration Examples

List<t> generic collections are used in a similar way to ArrayList collections.

2, Dictionary<k,v> class

Saves a specific element in the form of a key/value pair. In a generic collection of dictionary<k,v>, you can store data in a way similar to a hash table, and you can type-check the saved data. Any key in the dictionary must be unique and the key cannot be empty. However, if the value type is a reference type, the value type can be null.

New dictionary< key type, value type > ();

3, Stack<t> class

Similar to the Stack collection.

4, Queue<t> class

Similar to the queue collection.

Why generics are introduced into generics?

A parameterized type is implemented to manipulate multiple data types on the same piece of code. Use the parameterized type to abstract the type for flexible reuse.

The use of generics is flexible and widely used.

1. Generic class definition

Defining a generic class means defining a class in which the types of certain fields are indeterminate, which can be determined when the class is constructed.

class Classa<t1,t2>{    private  T1 A;     Private T2 b;      Public ClassA (T1 a,t2 b) {        this. A = a;  this. B = b;    }      Public string getsum () {        return A + "" +B;    }}
definition of generic class2. Generic method

Defining a generic method is defining a method, the type of the parameter of the method can be indeterminate, and when the method is called, the type of the parameter of the method is determined.

 Public Static T getsum<t>(t a,t b) {    return a +b;}
definition of a generic method

Indexer: Access to data through the form of [index], which is the indexer, is commonly used to access the method in generics.

C#_ Collections and generics

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.