Custom generic classes use the set and set interfaces provided by Microsoft.

Source: Internet
Author: User

// Implement the getenumerator () method in the ienumerable <A> interface, in order to support foreach Traversal
Class myclass <A>: ienumerable <A>
{
List <A> List = new list <A> ();

Private list <A> items;
Public list <A> items
{
Get {return list ;}
}

// Number of elements
Private int count;
Public int count
{
Get {return list. Count ;}
Set {COUNT = value ;}
}

Public void add (a item)
{
This. List. Add (item );
}

Public bool remove (a item)
{
Return this. List. Remove (item );
}

Public void removeat (INT index)
{
This. List. removeat (INDEX );
}

# Region ienumerable <A> Member

Public ienumerator <A> getenumerator ()
{
Return list. getenumerator ();
}

# Endregion

# Region ienumerable Member

System. Collections. ienumerator system. Collections. ienumerable. getenumerator ()
{
Throw new notimplementedexception ();
}

# Endregion
}

 

****************/
Static void main (string [] ARGs)
{
Myclass <student> myclass = new myclass <student> ();
Student stu1 = new student () {stuname = "Zhang San", stuno = "s001", stuage = 23 };
Student stu2 = new student () {stuname = "", stuno = "s002", stuage = 24 };
Myclass. Add (stu1 );
Myclass. Add (stu2 );
Console. writeline (myclass. Count );
Foreach (student item in myclass)
{
Console. writeline (item. stuname );
}
}

// Use
****************/
// Static void main (string [] ARGs)
//{
//// Integer and string can be compared to the size, so they can be sorted
/// Arraylist arr = new arraylist ();
/// Arr. Add ("AA ");
/// Arr. Add ("ad ");
/// Arr. Add ("AC ");
/// Arr. Add ("aw ");
/// Arr. Add ("ad ");
/// Foreach (VAR item in ARR)
////{
/// Console. writeline (item );
////}
/// Arr. Sort (); // sort
/// Console. writeline ("sorted :");
/// Foreach (VAR item in ARR)
////{
/// Console. writeline (item );
////}
//// Class
// List <student> arr = new list <student> ();
// Student stu1 = new student () {stuname = "zhangsan", stuno = "s001", stuage = 23 };
// Student stu2 = new student () {stuname = "", stuno = "s002", stuage = 14 };
// Student stu3 = new student () {stuname = "Wang Wu", stuno = "s003", stuage = 28 };
// Student stu4 = new student () {stuname = "Zhao ", stuno = "s004", stuage = 26 };
// Arr. Add (stu1 );
// Arr. Add (stu4 );
// Arr. Add (stu3 );
// Arr. Add (stu2 );
// Foreach (VAR item in ARR)
//{
// Console. writeline (item. stuno + "-" + item. stuname + "-" + item. stuage );
//}
/// Arr. Sort (); // sort (Implementation Interface)
// Arr. Sort (New agecomparerdesc (); // sort (comparator)
// Console. writeline ("sorted :");
// Foreach (VAR item in ARR)
//{
// Console. writeline (item. stuno + "-" + item. stuname + "-" + item. stuage );
//}
//}

 

Related Article

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.