C #2.0 generic -- dictionary, list usage

Source: Internet
Author: User
Tags mscorlib

Generics are a new feature in the C #2.0 language and Common Language Runtime Library (CLR. Generics introduce the concept of type parameters. net Framework, type parameters make it possible to design the following classes and Methods: these classes and Methods delay one or more types until the client Code declares and instantiate the class or method. For example, by using the generic type parameter T, you can write a single class that can be used by other client code without introducing the forced conversion or packing operation during runtime.
You can use generic types to maximize code reuse, protect type security, and improve performance.
The most common use of generics is to create a collection class.
The. NET Framework class library contains several new generic collection classes in the system. Collections. Generic namespace. Use these classes as much as possible to replace common classes, such as arraylist and hashtable in the system. Collections namespace.
The following describes the usage of several generic collection classes:

I. Dictionary
This class is added in. NET Framework 2.0. A set of keys and values. Namespace: system. Collections. Generic, assembly: mscorlib (in mscorlib. dll)
Class testgenericlist
{
Static void main ()
{
// Declare the object. The parameter indicates that the key is of the int type and the value is of the string type.
Dictionary <int, string> fruit = new dictionary <int, string> ();
Try {
// Adding the duplicate key will cause an exception.
Fruit. Add (1, "apple ");
Fruit. Add (2, "orange ");
Fruit. Add (3, "banana ");
Fruit. Add (4, "pineapple ");
// Parameter error will cause an exception, as shown below
// Fruit. Add ("5", "AA ");
}
Catch (argumentexception)
{
Console. writeline ("adding Error !!! ");
}
// Because generics are introduced, object-to-int conversion is not required after the key is taken out, and the set of values is the same.
Foreach (int I in fruit. Keys)
{
Console. writeline ("key: {0} value: {1}", I, fruit );
}
// Delete the specified key and Value
Fruit. Remove (1 );
// Determine whether the specified key is included
If (fruit. containskey (1 ))
{
Console. writeline ("include this key ");
}
// Clear all objects in the Set
Fruit. Clear ();
}
}
The order in which dictionary traverses the output is the order of addition. This is different from hashtable. Other methods include containskey, containsvalue, and remove. The usage is basically the same.

Ii. List class
Note: This class is added in. NET Framework 2.0. Lists the strong types of objects that can be accessed through indexes. Provides methods for searching, sorting, and operating the list. Namespace: system. Collections. Generic, assembly: mscorlib (in mscorlib. dll). The list class is the generic equivalent class of the arraylist class.

// Declare a generic class
Class testgenericlist
{
Static void main ()
{
// Declare a list object and add only the string parameter
List <string> names = new list <string> ();
Names. Add ("Qiao Feng ");
Names. Add ("Ouyang Feng ");
Names. Add ("Bee ");
// Traverse the list
Foreach (string name in names)
{
Console. writeline (name );
}
// Insert an element to the list
Names. insert (2, "Zhang Sanfeng ");
// Remove the specified Element
Names. Remove ("Bee ");
}
}
When deciding whether to use the list or the arraylist class (both have similar functions), remember that the List class performs better and is type-safe in most cases. If the type T of the List class is referenced, the behavior of the two classes is identical. However, if you use value type T, you need to consider implementation and packing.

If the value type T is used, the compiler will generate a list class for this value type. This means that you can use this element without packing the list element of the list object. After about 500 list elements are created, the memory saved by packing the list elements is greater than the memory used to generate this class.

You can also define a generic class as follows:
// Declare a generic class
Public class itemlist <t>
{
Void add (T item ){}
}
Class testgenericlist
{
Private class exampleclass {}
Static void main ()
{
// Declare an object. Only int type can be added.
Itemlist <int> list1 = new itemlist <int> ();

// Declare an object. Only the student type can be added. The student class is a custom class.
Itemlist <student> list2 = new itemlist <student> ();

}
}

There are many other generic methods, such as generic methods, generic delegation, and generic interfaces.

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.