asp.net the use of generics in C #

Source: Internet
Author: User


First, list<t> namespaces:

System.Collections.Generic (assembly: mscorlib)
Second, list<t> Description:
1), a strongly typed list of objects that can be accessed through the index, and a method for searching, sorting, and manipulating the list. 2), is the generic equivalent class of the ArrayList class. 3, you can access the elements in this collection using an integer index, and the index starts at zero. 4, you can receive a null reference (Nothing in VB). 5), allow duplicate elements

Three, list<t> constructor function

Name Description
List<t> () Initializes a new instance of the List<t> class that is empty and has the default initial capacity (0).
List<t> (ienumerable<t>) Initializes a new instance of the List<t> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the copied elements.
List<t> (Int32) Initializes a new instance of the List<t> class that is empty and has the specified initial capacity.
Note: When you add an element to list<t> by default, the capacity is automatically increased as needed by reallocating the internal array. If you can estimate the size of the collection, when you specify the initial capacity, you do not need to perform a large number of resizing operations when adding elements to the list<t>. This can improve performance.

Four, list<t> property

Name Description
Capacity Gets or sets the total number of elements that the internal data structure can hold without resizing.
Count gets the number of elements actually contained in the list<t>.
Description: Capacity is the number of elements that list<t> can store before it needs to be resized, and count is the number of elements actually stored in list<t>.

Five, list<t> method


Name Description

Add adds an object to the end of the list<t>.
AddRange adds the elements of the specified collection to the end of the list<t>.
AsReadOnly returns the read-only ilist<t> wrapper for the current collection.
BinarySearch (T) searches for an element in the entire sorted list<t> using the default comparer and returns the zero-based index of the element.
BinarySearch (T, icomparer<t>) searches for an element in the entire sorted list<t> using the specified comparer and returns the zero-based index of the element.
BinarySearch (Int32, Int32, T, icomparer<t>) uses the specified comparer to search for an element in a range of elements in the sorted list<t> and returns the zero-based index of the element.
Clear removes all elements from the list<t>.
Contains determines whether an element is in list<t>.
Convertall<toutput> converts the elements in the current list<t> to a different type and returns a list containing the converted elements.
CopyTo (t[]) copies the entire list<t> into a compatible one-dimensional array, starting at the beginning of the target array.
Exists Determines whether the list<t> contains elements that match the conditions defined by the specified predicate.
Find searches for elements that match the conditions defined by the specified predicate and returns the first matching element in the entire list<t>.
FindIndex (predicate<t>) searches for elements that match the conditions defined by the specified predicate and returns the zero-based index of the first matching element in the entire list<t>.
ForEach performs the specified operation on each element of the list<t>.
GetEnumerator Returns an enumerator that iterates through the list<t>.
IndexOf (T) searches for the specified object and returns the zero-based index of the first occurrence in the entire list<t>.
Insert inserts an element into the list<t> at the specified index.
Insertrange inserts an element from the collection into the list<t> at the specified index.
LastIndexOf (T) searches for the specified object and returns the zero-based index of the last occurrence in the entire list<t>.
Remove removes the first occurrence of a particular object from the list<t>.
Reverse () Reverses the order of the elements in the entire list<t>.
Sort () sorts the elements in the entire list<t> using the default comparer.
TrimExcess sets the capacity to the actual number of elements in list<t> if the number is less than a threshold value.
Trueforall determines whether each element in list<t> matches the criteria defined by the specified predicate.
Note: The above method description is useful to "predicate", which is the Predicate<t> delegate, which represents a set of methods that define a set of conditions and determine whether the specified parameter objects meet these conditions, as shown in the sample program.

namespaces: Using System.Collections.Generic;

Normal array: The length and data type of the array must be formulated at the time of declaration; ArrayList: array element data types and lengths are unrestricted, but less efficient; generics: array-like functionality, with unlimited length and data types that must be specified when declared.

C # has predefined generic classes that are commonly used, such as:list<t>, Dictionary<tkey, tvalue>, queue<t>, and so on

1. list<t>

Grammar:
list< type > variable = new list< type > ();
list< type > variable = new list< type > {xxx, xxx, xxx}; Declare and initialize
Example 1: Simple and practical: declaring, initializing, assigning, reading, and so on. As can be seen in the following example, variable List1 can be arbitrarily added, with unlimited length.

Declaration Mode 1
List list1 = new list ();
List1. ADD (211);
List1. ADD (985);

Declaration Mode 2
List List2 = new List {211, 985};
List2. ADD (136);

for (int i = 0; i < List2. Count; i++)
{
Console.Write (List2[i]. ToString ());
}
2. Dictionary<tkey, tvalue>

The elements stored in the generic variable list1 in the example above are variables of type int, and are often more complex than this in practical applications. Dictionary<tkey, the tvalue> generic class is a C # predefined generic class, and each element it stores consists of a pair of {key:value} two variables.

Each element can be interpreted as a single line of records, the key as the primary key, must be unique, can be int, string, and other types.
The value item can be of any type, can be int, String, array, and so on, or it can be an instantiated object.
① Simple to use

Class Program
{
static void Main (string[] args)
{
Instantiating three user objects
User User1 = new User ("Li Ning", 21);
User User2 = new User ("Nike", 42);
User User3 = New User ("Artie", 31);

Declare dictionary generic variable users
The type of key is Int;value type is user
Dictionary<int, user> users = new Dictionary<int, user> ();

Add "element" to the users, "element" key cannot be duplicated
Users. ADD (2, User1);
Users. ADD (3, user2);
Users. ADD (5, User3);

       //looping through each "element"
        foreach (var p in users)
        {
             Console.Write (P.key);
            Console.Write (p.value.name);
            Console.Write (p.value.age);
       }

Values that traverse elements directly
foreach (User u in users.) Values)
{
Console.Write (u.name);
Console.Write (U.age);
}
}
}
User class
Class User
{
public string name; Name
public int age; Age
Public User (string name, int age)
{
THIS.name = name;
This.age = age;
}
}
② used as a parameter

Class Program
{
static void Main (string[] args)
{
User User1 = new User ("Li Ning", 21);
User User2 = new User ("Nike", 42);
User User3 = New User ("Artie", 31);

Dictionary<int, user> users = new Dictionary<int, user> ();
Users. ADD (2, User1);
Users. ADD (3, user2);
Users. ADD (5, User3);

Program.show (users); To use a generic variable as a function parameter
}
Note the writing format for parameter types
static void Show (Dictionary<int, user> list)
{
foreach (var p in list)
{
Console.Write (P.key);
Console.Write (P.value.name);
Console.Write (P.value.age);
}
}
}
Class User
{
public string name; Name
public int age; Age
Public User (string name, int age)
{
THIS.name = name;
This.age = age;
}
}

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.