C # reference: Generic (3)-Application generic

Source: Internet
Author: User
Because IL and CLR provide native support for generics, most CLR-compliant languages can use general types. For example, the following section of Visual Basic. NET Code uses Code Block 2General stack: Dim Stack As stack (Of Integer)
Stack = new Stack (Of Integer)
Stack. Push (3)
Dim number As Integer
Number = stack. Pop ()

You can use generics in classes and structures. The following is a useful general Point structure: public struct Point
{

Public t x;

Public t y;
}

The general point can be used to represent integer coordinates, for example:

Point point;
Point. X = 1;
Point. Y = 2;

Alternatively, you can use it to represent the chart coordinates that require floating-point precision:

Point point;
Point. X = 1.2;
Point. Y = 3.4;

In addition to the basic generic syntax introduced so far, C #2.0 also has some generic-specific syntaxes.
For example, considerCode Block 2 OfPop ()Method. Assume that you do not want to raise an exception when the stack is empty, but want to return the default value of the type stored in the stack. If you use an Object-based stack, you can simply return null, but you can also use a general stack by using the value type. To solve this problem, you can useDefault () Returns the default value of the type.

The following describes howPop ()The default value is used in the implementation of the method:

Public T Pop ()
{
M_StackPointer --;
If (m_StackPointer> = 0)
{
Return m_Items [m_StackPointer];
}
Else
{
M_StackPointer = 0;
Return default (T );
}
}

The default value of the reference type is null, and the default value of the value type (such as integer, enumeration, and structure) is all zero (fill the corresponding structure with zero ). Therefore, if the stack is constructed using stringsPop ()Method returns null when the stack is empty. If the stack is constructed with an integerPop ()Method returns zero if the stack is empty.

Multiple general types:
A single type can define multiple general type parameters. For example, considerCode block 3 The general linked list shown in.

Code Block 3. General linked list

Class Node
{
Public K Key;
Public T Item;
Public Node NextNode;
Public Node ()
{
Key = default (K );
Item = defualt (T );
NextNode = null;
}
Public Node (K key, T item, Node nextNode)
{
Key = key;
Item = item;
NextNode = nextNode;
}
}

Public class upload list
{
Node m_Head;
Public writable list ()
{
M_Head = new Node ();
}
Public void AddHead (K key, T item)
{
Node newNode = new Node (key, item, m_Head.NextNode );
M_Head.NextNode = newNode;
}
}

The linked list storage Node: class Node
{}

Each node contains a key (a common type parameter K) and a value (a common type parameter T ). Each node also has a reference to the next node in the list. The linked list itself is defined according to the common type parameters K and T: public class shortlist
{}

This makes the list public.AddHead ()The same general method:

Public void AddHead (K key, T item );

Each time you declare a variable of the generic type, you must specify the type to be used. However, the specified type parameter can be a common type parameter. For example, the linked list has a Node-type member variable named m_Head, which is used to reference the first item in the list. M_Head is declared using its own general type parameters K and T.

You need to provide type arguments when instantiating nodes. Similarly, you can use the Linked List's own general type parameters:

public void AddHead(K key,T item){Node newNode = new Node<K,T>(key,item,m_Head.NextNode);m_Head.NextNode = newNode;}

Note that the list uses the same name as the node to indicate that a common type parameter is used to improve readability. Other names can also be used, for example:

public class LinkedList{...}

Or:

public class LinkedList{...}

In this case, the m_Head is declared:

Node m_Head;

When the client uses the linked list, the client must provide type arguments. The client can select an integer as the key and a string as the data item:

LinkedList list = new LinkedList();list.AddHead(123,"AAA");

However, the client can select any other combination (for example, timestamp) to represent the key:

LinkedList list = new LinkedList();list.AddHead(DateTime.Now,"AAA");

Sometimes it is useful to create aliases for special combinations of specific types. You can use the using statement to complete this operation, as shown in figureCode block 4. Note that the scope of the alias is the scope of action of the file. Therefore, you must repeatedly create an alias in the project file in the same way as using the using namespace.

Code block 4. General type alias

Using List = sort List;

Class ListClient
{
Static void Main (string [] args)
{
List list = new List ();
List. AddHead (123, "AAA ");
}
}

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.