Sorting and Searching:Overloading Relational Operators : Operator Overloading « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Operator OverloadingScreenshots 
Sorting and Searching:Overloading Relational Operators
Sorting and Searching:Overloading Relational Operators

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 28 - System.Array and the Collection Classes\Sorting and Searching\Overloading Relational Operators
// copyright 2000 Eric Gunnerson
using System;

public class OverloadingRelationalOperators
{
    public static void Main()
    {
        Employee george = new Employee("George"1);
        Employee fred = new Employee("Fred"2);
        Employee tom = new Employee("Tom"4);
        Employee bob = new Employee("Bob"3);
        
        Console.WriteLine("George < Fred: {0}", george < fred);
        Console.WriteLine("Tom >= Bob: {0}", tom >= bob);
    }

public class Employee: IComparable
{
    public Employee(string name, int id)
    {
        this.name = name;
        this.id = id;
    }
    
    int IComparable.CompareTo(object obj)
    {
        Employee emp2 = (Employeeobj;
        if (this.id > emp2.id)
        return(1);
        if (this.id < emp2.id)
        return(-1);
        else
        return(0);
    }
    public static bool operator <(
    Employee emp1,
    Employee emp2)
    {
        IComparable    icomp = (IComparableemp1;
        return(icomp.CompareTo (emp20);
    }
    public static bool operator >(
    Employee emp1,
    Employee emp2)
    {
        IComparable    icomp = (IComparableemp1;
        return(icomp.CompareTo (emp20);
    }
    public static bool operator <=(
    Employee emp1,
    Employee emp2)
    {
        IComparable    icomp = (IComparableemp1;
        return(icomp.CompareTo (emp2<= 0);
    }
    public static bool operator >=(
    Employee emp1,
    Employee emp2)
    {
        IComparable    icomp = (IComparableemp1;
        return(icomp.CompareTo (emp2>= 0);
    }
    
    public override string ToString()
    {
        return(name + ":" + id);
    }
    
    string    name;
    int    id;
}

           
       
Related examples in the same category
1.An example of operator overloadingAn example of operator overloading
2.More operator overloadingMore operator overloading
3.Overload addition for object + object, and for object + intOverload addition for object + object, and     for object + int
4.Overload the + for object + object, object + int, and int + objectOverload the + for object + object,     object + int, and int + object
5.Overload shift operatorOverload shift operator
6.Overload true and fase for ThreeDOverload true and fase for ThreeD
7.A better way to overload !, | and & for ThreeD. This version automatically enables the && and || operatorsA better way to overload !, | and & for ThreeD.     This version automatically enables the && and || operators
8.illustrates operator overloadingillustrates operator overloading
9.Demonstrates overloading the addition operator for two class objectsDemonstrates overloading the addition operator for two class objects
10.overloaded operator + takes two fractionsoverloaded operator + takes two fractions
11.Overloaded operator: whether two Fractions are equalOverloaded operator: whether two Fractions are equal
12.Overload != operatorOverload != operator
13.Operator Overloading:An ExampleOperator Overloading:An Example
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.