Ylbtech-unitity-cs:generics

Source: Internet
Author: User

Ylbtech-unitity-cs:generics

1.A, back to top
Unsorted list:raul:35alessandro:30maria:72hiroyuki:108alok:9gunnar:18sandra:23li:28bill:19franscoise:45sorted List:alok:9gunnar:18bill:19sandra:23li:28alessandro:30raul:35franscoise:45maria:72hiroyuki:108done Please press any key to continue ...
View Code
1.B, source code back to top
1.b.1,
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Text;namespacegenerics_csharp{//the type parameter T in angle brackets.      Public classMylist<t>: ienumerable<t>    {        protectedNode Head; protectedNode current =NULL; //nested types are also generics on T        protected classNode { PublicNode Next; //T as the private member data type.             PrivateT data; //The T used in a non-generic constructor.              PublicNode (t) {next=NULL; Data=T; }             PublicNode Next {Get{returnNext;} Set{next =value;} }            //T as the return type of the property.              PublicT Data {Get{returndata;} Set{data =value;} }        }         PublicMyList () {head=NULL; }        //T as a method parameter type.          Public voidAddHead (T t) {Node n=NewNode (t); N.next=Head; Head=N; }        //implement GetEnumerator to return IENUMERATOR<T>, enabling the list's//foreach iteration. Please note that in C # 2.0,//you do not need to implement current and MoveNext. //The compiler will create a class that implements Ienumerator<t>.          PublicIenumerator<t>GetEnumerator () {Node current=Head;  while(Current! =NULL)            {                yield returnCurrent .                Data; Current=Current .            Next; }        }        //This method must be implemented, because//ienumerable<t> Inheritance IEnumerableIEnumerator Ienumerable.getenumerator () {returnGetEnumerator (); }    }     Public classSortedlist<t>: mylist<t>whereT:icomparable<t>    {        //a simple sorting algorithm that is not optimized,//the algorithm sorts the list elements from low to High:         Public voidBubblesort () {if(NULL= = Head | |NULL==head. Next)return; BOOLswapped;  Do{Node Previous=NULL; Node Current=Head; Swapped=false;  while(Current.next! =NULL)                {                    //because this method needs to be called, SortedList//class is bound on ienumerable<t>                    if(Current.Data.CompareTo (Current.next.Data) >0) {Node tmp=Current.next; Current.next=Current.next.next; Tmp.next=Current ; if(Previous = =NULL) {Head=tmp; }                        Else{Previous.next=tmp; } Previous=tmp; Swapped=true; }                    Else{Previous=Current ; Current=Current.next; }                }//End While} while(swapped); }    }    //a simple class that implements icomparable<t> itself as a type parameter,//is in the object.//Common Design patterns, these objects//stored in the generic list.      Public classPerson:icomparable<person>    {        stringname; intAge ;  PublicPerson (stringSinti) {name=s; Age=i; }        //This causes the list element//sort by age value.          Public intCompareTo (person p) {returnAge-P.age; }         Public Override stringToString () {returnName +":"+Age ; }        //Equals must be implemented.          Public BOOLEquals (person p) {return( This. Age = =p.age); }    }    classGenerics {Static voidMain (string[] args) {            //declares and instantiates a new paradigm SortedList class. //Person is a type parameter. sortedlist<person> list =NewSortedlist<person>(); //Create a name and age value to initialize the person object.             string[] names =New string[] {"franscoise","Bill","Li","Sandra","Gunnar","Alok","Hiroyuki","Maria","Alessandro","Raul" }; int[] ages =New int[] { $, +, -, at, -,9,108, the, -, * }; //populate the list.              for(intx =0; x < names. Length; X + +) {list. AddHead (NewPerson (Names[x], ages[x])); } Console.WriteLine ("unsorted List:"); //prints a list that is not sorted.             foreach(Person Pinchlist)            {Console.WriteLine (p.tostring ()); }            //sort the list. list.            Bubblesort (); Console.WriteLine (String.Format ("{0}sorted List:", Environment.NewLine)); //print out the sorted list.             foreach(Person Pinchlist)            {Console.WriteLine (p.tostring ()); } Console.WriteLine (" Done"); }    }}
1.b.2,
1.C, back to top

Ylbtech
Source: http://ylbtech.cnblogs.com/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Ylbtech-unitity-cs:generics

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.