C # icomparable generic interface

Source: Internet
Author: User

Public class student: icomparable <student>
{
Private string name;
Private int age;
Public student (string _ name, int _ age)
{
Name = _ name;
Age = _ age;
}
Public string name
{
Get
{
Return this. Name;
}
Set
{
This. Name = value;
}
}
Public int age
{
Get
{
Return this. Age;
}
Set
{
This. Age = value;
}
}

Public override string tostring ()
{
Return string. Format ("{0}, {1}", this. Name, this. Age );
}
# Region icomparable Member

Public int compareto (student other)
{
Return this. Age. compareto (other. Age );
}

# Endregion
}

// Calls in main

 

Student son = new student ("son", 23 );
Student Tom = new student ("Tom", 45 );
Student anle = new student ("anle", 32 );
List <student> List = new list <student> ();
List. Add (son );
List. Add (Tom );
List. Add (anle );
Console. writeline ("----- before sorting -----");
Foreach (student s in List)
{
Console. writeline (s );
}

List. Sort ();
Console. writeline ("----- after sorting -----");
Foreach (student s in List)
{
Console. writeline (s );
}

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.