Type comparison:
When comparing objects, it is often necessary to know their types in order to determine whether a comparison of values can be made.
Method one.
You can use the GetType () method with the typeof () operator to determine the type of the object.
Example:
int i = ten; if (i.gettype () = = typeof (Int32)) { Response.Write ("Type of Int32"); }
Method Two:
Is operator.
The IS operator is not a way of stating that an object is of a certain type, but rather to check whether the object is of the given type, or whether it can be converted to the given type, and if so, returns True.
Grammar:
<operand> is <type>
The results are as follows:
| Type |
Operand |
Result |
| Class type |
is also a class type, or inherits the class, or marshals to the class |
True |
| Interface type |
is also the interface type, or implements the interface |
True |
| Value type |
is also a value type, or unpacking to that type |
True |
Example:
public struct ms{}
protected void Page_Load (object sender, EventArgs e) { ms M = new MS (); If (M is Ms) { Response.Write ("Type of Ms<br/>"); } Object o = m; If (O is Ms) { Response.Write ("Type of Ms<br/>"); } }
Results:
Value comparison:
When considering comparing two person objects, each object has an age attribute, comparing two people to see who is older. The code is as follows:
if (Person1. Age>person2. Age) {}
You can also use operator overloading.
if (Person1>person2) {}
Compared to two objects, you can also compare weight, height and other properties.
Another option is to use the IComparable and IComparer interfaces. They can define the process of comparing two objects in a standard way.
The difference between the two interfaces:
IComparable is implemented in the class of the object to compare, you can compare the object and another object.
The IComparer is implemented in a separate class and can be arbitrarily compared to two objects.
In general, we use IComparable to give the class the default comparison code, using other classes to give a non-default comparison code.
IComparable:
Summary: // defines a common comparison method that is implemented by a value type or class to create a type-specific comparison method. [ComVisible (true)] public interface IComparable { //Summary: // Compare the current instance with another object of the same type. //// Parameters: // obj: // object to compare with this instance. ///// Returns the result: // A 32-bit signed integer indicating the relative order of the objects to compare. The meaning of the return value is as follows: The value meaning is less than 0 this instance is less than obj. 0 this instance equals obj. // greater than 0 this instance is greater than obj. ///// exception: / /System.ArgumentException: // obj does not have the same type as this instance. int CompareTo (object obj); }
Example:
public class person:icomparable{public int age; public string Name; public person (int age,string name) {this . name = name; This. Age = Age; } public int CompareTo (object obj) {person p = obj as person; Return p.age-this. age; }}
IComparer:
Summary: // exposes a method that compares two objects. [ComVisible (true)] public interface IComparer { //Abstract: // compare two objects and return a value indicating that an object is less than, is equal to or greater than the other object. //// Parameters: // x:// The first object to compare. //// y: // The second object to compare. //// return Result: // value condition less than 0 x less than Y. 0 x equals Y. Greater than 0 x is greater than Y. ///// exception: / /System.ArgumentException: // x and Y do not implement System.IComparable interface. -or-the types of x and Y are different, and none of them can handle comparisons with another. int Compare (object x, object y); }
Example:
public class person{public int age; public string Name; public person (int age,string name) {this . name = name; This. Age = Age; }} public class personcompare:icomparer{Public int Compare (object x, object y) {person p1 = x as Person;
person P2 = y as person; return P1. Age-p2. age; }}
protected void Page_Load (object sender, EventArgs e) {person p1 = new Person (10, "Zhang San"); person P2 = new person (20, "John Doe"); Personcompare pc = new Personcompare (); int result = Pc.compare (P1, p2); Response.Write ("P1:" + p1. Name + "--" + P1. Age + "<br/>"); Response.Write ("P2:" + p2. Name + "--" + P2. Age + "<br/>"); if (result>0) { Response.Write (P1. Name + "Age >" + p2. Name); } else if (result==0) { Response.Write (P1. Name + "age =" + P2. Name); } else { Response.Write (p1). Name + "Age <" + P2. Name); } }
c#--comparison