Generic--1 of C # Advanced programming Create generic classes

Source: Internet
Author: User

. NET has been supporting generics since version 2.0.

    • Non-generic linked list

Gossip Hugh, now look at the non-generic simplified list class, which can contain any type of object.

In LinkedListNode.cs:

In a linked list, an element references another element, so you must create a class that encapsulates it in a linked list and references the next object.

1      Public classLinkedListNode2     {3          PublicLinkedListNode (Objectvalue)4         {5              This. Value =value;6         }7 8          Public ObjectValue {Get;Private Set; }9 Ten          PublicLinkedListNode Next {Get;Internal Set; } One          PublicLinkedListNode Prev {Get;Internal Set; } A}

In LinkedListNode.cs:

The LinkedList class contains the first of the LinkedListNode type, with the last attribute, which respectively marks the tail and the ends of the linked list. By implementing the GetEnumerator () method, you can traverse a linked list with a foreach statement.

1      Public classlinkedlist:ienumerable2     {3          PublicLinkedListNode First {Get;Private Set; }4          PublicLinkedListNode last {Get;Private Set; }5 6          PublicLinkedListNode AddLast (Objectnode)7         {8             varNewNode =NewLinkedListNode (node);9             if(First = =NULL)Ten             { OneFirst =NewNode; ALast =First ; -             } -             Else the             { -Last.next =NewNode; -Last =NewNode; -             } +             returnNewNode; -         } +  A          PublicIEnumerator GetEnumerator () at         { -LinkedListNode current =First ; -              while(Current! =NULL) -             { -                 yield returnCurrent . Value; -Current =Current . Next; in             } -}

In Program.cs:

1             varList1 =NewLinkedList ();2List1. AddLast (2);3List1. AddLast (4);4List1. AddLast ("6");5 6             foreach(intIinchList1)7             {8 Console.WriteLine (i);9}

At this point, a run exception occurs because an exception occurs when the 3rd element in the list is converted to an integer.

    • generic linked list

To avoid this situation, create a generic list below.

In LinkedListNode.cs:

The LinkedListNode class is declared with a generic type T. The type of property value is the, not an object.

     Public classLinkedlistnode<t>   {       PublicLinkedListNode (T value) { This. Value =value; }          PublicT Value {Get;Private Set; }  PublicLinkedlistnode<t> Next {Get;Internal Set; }  PublicLinkedlistnode<t> Prev {Get;Internal Set; } }

In LinkedList.cs:

The LinkedList is also defined as a generic class.


1 Public classLinkedlist<t>: ienumerable<t>2 {3 PublicLinkedlistnode<t> First {Get;Private Set; }4 PublicLinkedlistnode<t> last {Get;Private Set; }5 6 PublicLinkedlistnode<t>AddLast (T node)7 {8 varNewNode =NewLinkedlistnode<t>(node);9 if(First = =NULL)Ten { OneFirst =NewNode; ALast =First ; - } - Else the { -Last.next =NewNode; -Last =NewNode; - } + returnNewNode; - } + A PublicIenumerator<t>GetEnumerator () at { -Linkedlistnode<t> current =First ; - - while(Current! =NULL) - { - yield returnCurrent . Value; inCurrent =Current . Next; - } to } + - IEnumerator ienumerable.getenumerator () the { * returnGetEnumerator (); $ }Panax Notoginseng}

In Program.cs:

1     class Program2     {3         Static voidMain ()4         {5             varList2 =Newlinkedlist<int>();6List2. AddLast (1);7List2. AddLast (3);8List2. AddLast (5);9 Ten             foreach(intIinchlist2) One             { A Console.WriteLine (i); -             } -  the             varList3 =Newlinkedlist<string>(); -List3. AddLast ("2"); -List3. AddLast (" Four"); -List3. AddLast (NULL); +  -             foreach(stringSinchlist3) +             { A Console.WriteLine (s); at             } - Console.read (); -  -}

foreach is now type-safe.

Generic--1 of C # Advanced programming Create generic classes

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.