. Comparison and sequencing of the series of programming Interface analysis

Source: Internet
Author: User
Tags comparison sort tostring

As we know, C # as well as the whole is compared to C + +. NET does not support multiple inheritance, and accordingly, C # supports interfaces and supports a type to implement multiple interfaces. For the concept of interface, I believe that most readers have a good understanding, and I am here to talk about the personal understanding of the interface, just a comment.

In my opinion, an interface is an authentication of a certain kind of ability, and it is in some standardized form to the specification of this ability. Your type implements an interface, in other words, that type has the capabilities that this interface identifies. For example, now study abroad TOEFL GRE, driving a driver's license. These things, in fact, is equivalent to our programming interface; In a sense, you pass the GRE, which means that you have the language skills you need to study abroad, and you get a driver's license, which proves that you have the ability to drive on the road. The same is true for interfaces that give you the type to implement certain interfaces, that is, to mark them with special capabilities, and some capabilities that depend on these capabilities, can be reused in common code and are extensible.

My series of articles on interfaces is mostly right. NET programming a number of very important interfaces for detailed explanation, in-depth understanding of the principles and applications of these interfaces. This is very helpful for us to write concise and graceful code, after all, when we know what we want to do, we should first know what the. Net framework can do for us.

In this and subsequent articles we will talk about the following topics:

(i) Comparison and sequencing (IComparable and IComparer)

(ii) enumeration (IEnumerable and IEnumerator)

(iii) serialization (ISerializable and IXmlSerializable)

System.IComparable & System.icomparable<t>

As the name suggests, a class that implements IComparable should be a class that can compare instances to each other, so let's take a look at its definition first:

[ComVisible(true)]
public interface IComparable
{
int CompareTo(object obj);
}

This interface is fairly simple and provides only one interface function: CompareTo, returns a negative number if the current object is smaller than the object being compared, returns 0 if it is equal, or returns a positive number if the current object is larger than the object being compared.

However, if you think that this interface is only able to make you compare the size of two objects, then you are wrong, this interface is more important to achieve the type of linear data structure of the sorting function. such as List<t> Sort () and array static method sort can make good use of icomparable to sort the data, the sorting algorithm is implemented by the class library, for us, we only need to let our type implement IComparable interface, it is OK to compare two object size algorithm.

Icomparable<t> is a generic interface that implements comparisons to specific types of objects, uses and IComparable are basically the same, no more repeating here, and the following examples are written according to IComparable.

Let's take a look at the following code, which defines a student class student, each student having his or her own name and score. The student class implements the IComparable interface, and the two students are compared directly to each other by name. Incidentally, the scores class is used to store student scores.

public enum Subjectenum
{
Total = 0,
Chinese,
中文版,
Math,
}
  
public class Scores//fractional class, for storing fractions
{
int[] _score = new Int[4];
public int This[subjectenum Score]
{
get {return _score[(int) score];}
set {_score[(int) score] = value;}
}
public override string ToString ()
{
String str = "";
foreach (int score in _score)
{
STR + + "" + score. ToString ();
}
  
return str;
}
}
  
public class Student:icomparable//Student class
{
  
string _name;
  
public string Name
{
get {return _name;}
set {_name = value;}
}
  
Scores _scores=new Scores ();
  
Public Scores Scores
{
get {return _scores;}
set {_scores = value;}
}
  
Public Student (String name,int Chinese, int 中文版, int math)
{
_name = name;
  
_scores[subjectenum.chinese] = Chinese;
_scores[subjectenum.english] = 中文版;
_scores[subjectenum.math] = Math;
_scores[subjectenum.total] = Chinese +english +math;
}
  
public override string ToString ()
{
return _name + _scores. ToString ();
}
  
#region IComparable Members
  
public int CompareTo (object obj)
{
if (!) ( obj is Student)
throw new ArgumentException ("Argument not a Student", "obj");
  
Return Name.compareto ((Student) obj). Name);
}
  
#endregion
}

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.