public class MethodOverloadError { public int Square( int x ) { return x * x; } public double Square( int y ) { return y * y; } }
Class Interface
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 ClassesSorting and SearchingOverloading 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 = (Employee) obj; 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 = (IComparable) emp1; return(icomp.CompareTo (emp2) < 0); } public static bool operator >( Employee emp1, Employee emp2) { IComparable icomp = (IComparable) emp1; return(icomp.CompareTo (emp2) > 0); } public static bool operator <=( Employee emp1, Employee emp2) { IComparable icomp = (IComparable) emp1; return(icomp.CompareTo (emp2) <= 0); } public static bool operator >=( Employee emp1, Employee emp2) { IComparable icomp = (IComparable) emp1; return(icomp.CompareTo (emp2) >= 0); } public override string ToString() { return(name + ":" + id); } string name; int id; }
Operator Overloading:An Example
/* A Programmer's Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3 */ // 25 - Operator OverloadingAn Example // copyright 2000 Eric Gunnerson using System; struct RomanNumeral { public RomanNumeral(int value) { this.value = value; } public override string ToString() { return(value.ToString()); } public static RomanNumeral operator -(RomanNumeral roman) { return(new RomanNumeral(-roman.value)); } public static RomanNumeral operator +( RomanNumeral roman1, RomanNumeral roman2) { return(new RomanNumeral( roman1.value + roman2.value)); } public static RomanNumeral operator ++( RomanNumeral roman) { return(new RomanNumeral(roman.value + 1)); } int value; } public class OperatorOverloadingAnExample { public static void Main() { RomanNumeral roman1 = new RomanNumeral(12); RomanNumeral roman2 = new RomanNumeral(125); Console.WriteLine("Increment: {0}", roman1++); Console.WriteLine("Addition: {0}", roman1 + roman2); } }
Overload != operator
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; class Fraction { private int numerator; private int denominator; // create a fraction by passing in the numerator // and denominator public Fraction(int numerator, int denominator) { this.numerator=numerator; this.denominator=denominator; } // overload the constructor to create a // fraction from a whole number public Fraction(int wholeNumber) { Console.WriteLine("In constructor taking a whole number"); numerator = wholeNumber; denominator = 1; } // convert ints to Fractions implicitly public static implicit operator Fraction(int theInt) { Console.WriteLine("Implicitly converting int to Fraction"); return new Fraction(theInt); } // convert Fractions to ints explicitly public static explicit operator int(Fraction theFraction) { Console.WriteLine("Explicitly converting Fraction to int"); return theFraction.numerator / theFraction.denominator; } // overloaded operator + takes two fractions // and returns their sum public static Fraction operator+(Fraction lhs, Fraction rhs) { // like fractions (shared denominator) can be added // by adding thier numerators if (lhs.denominator == rhs.denominator) { return new Fraction(lhs.numerator+rhs.numerator, lhs.denominator); } // simplistic solution for unlike fractions // 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8 // this method does not reduce. int firstProduct = lhs.numerator * rhs.denominator; int secondProduct = rhs.numerator * lhs.denominator; return new Fraction( firstProduct + secondProduct, lhs.denominator * rhs.denominator ); } // test whether two Fractions are equal public static bool operator==(Fraction lhs, Fraction rhs) { if (lhs.denominator == rhs.denominator && lhs.numerator == rhs.numerator) { return true; } // code here to handle unlike fractions return false; } // delegates to operator == public static bool operator !=(Fraction lhs, Fraction rhs) { bool equality = lhs==rhs; return !(equality); } // tests for same types, then delegates public override bool Equals(object o) { if (! (o is Fraction) ) { return false; } return this == (Fraction) o; } // return a string representation of the fraction public override string ToString() { String s = numerator.ToString() + "/" + denominator.ToString(); return s; } } public class TesterOverrideThree { static void Main() { Fraction f1 = new Fraction(3,4); Fraction f2 = new Fraction(2,4); Fraction f3 = f1 + f2; Console.WriteLine("adding f3 + 5..."); Fraction f4 = f3 + 5; Console.WriteLine("f3 + 5 = f4: {0}", f4.ToString()); Console.WriteLine(" Assigning f4 to an int..."); int truncated = (int) f4; Console.WriteLine("When you truncate f4 you get {0}", truncated); } }
Overloaded operator: whether two Fractions are equal
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; class Fraction { private int numerator; private int denominator; // create a fraction by passing in the numerator // and denominator public Fraction(int numerator, int denominator) { this.numerator=numerator; this.denominator=denominator; } // overloaded operator+ takes two fractions // and returns their sum public static Fraction operator+(Fraction lhs, Fraction rhs) { // like fractions (shared denominator) can be added // by adding thier numerators if (lhs.denominator == rhs.denominator) { return new Fraction(lhs.numerator+rhs.numerator, lhs.denominator); } // simplistic solution for unlike fractions // 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8 // this method does not reduce. int firstProduct = lhs.numerator * rhs.denominator; int secondProduct = rhs.numerator * lhs.denominator; return new Fraction( firstProduct + secondProduct, lhs.denominator * rhs.denominator ); } // test whether two Fractions are equal public static bool operator==(Fraction lhs, Fraction rhs) { if (lhs.denominator == rhs.denominator && lhs.numerator == rhs.numerator) { return true; } // code here to handle unlike fractions return false; } // delegates to operator == public static bool operator !=(Fraction lhs, Fraction rhs) { return !(lhs==rhs); } // tests for same types, then delegates public override bool Equals(object o) { if (! (o is Fraction) ) { return false; } return this == (Fraction) o; } // return a string representation of the fraction public override string ToString() { String s = numerator.ToString() + "/" + denominator.ToString(); return s; } } public class TesterOperatorOverride { static void Main() { Fraction f1 = new Fraction(3,4); Console.WriteLine("f1: {0}", f1.ToString()); Fraction f2 = new Fraction(2,4); Console.WriteLine("f2: {0}", f2.ToString()); Fraction f3 = f1 + f2; Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString()); Fraction f4 = new Fraction(5,4); if (f4 == f3) { Console.WriteLine("f4: {0} == F3: {1}", f4.ToString(), f3.ToString()); } if (f4 != f2) { Console.WriteLine("f4: {0} != F2: {1}", f4.ToString(), f2.ToString()); } if (f4.Equals(f3)) { Console.WriteLine("{0}.Equals({1})", f4.ToString(), f3.ToString()); } } }
overloaded operator + takes two fractions
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; class Fraction { private int numerator; private int denominator; // create a fraction by passing in the numerator // and denominator public Fraction(int numerator, int denominator) { this.numerator=numerator; this.denominator=denominator; } // overloaded operator + takes two fractions // and returns their sum public static Fraction operator+(Fraction lhs, Fraction rhs) { // like fractions (shared denominator) can be added // by adding their numerators if (lhs.denominator == rhs.denominator) { return new Fraction(lhs.numerator+rhs.numerator, lhs.denominator); } // simplistic solution for unlike fractions // 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8 // this method does not reduce. int firstProduct = lhs.numerator * rhs.denominator; int secondProduct = rhs.numerator * lhs.denominator; return new Fraction( firstProduct + secondProduct, lhs.denominator * rhs.denominator ); } // return a string representation of the fraction public override string ToString() { String s = numerator.ToString() + "/" + denominator.ToString(); return s; } } public class TesterOverrideToString { static void Main() { Fraction f1 = new Fraction(3,4); Console.WriteLine("f1: {0}", f1.ToString()); Fraction f2 = new Fraction(2,4); Console.WriteLine("f2: {0}", f2.ToString()); Fraction f3 = f1 + f2; Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString()); } }
Demonstrates overloading the addition operator for two class objects
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Plus.cs -- demonstrates overloading the addition operator for two // class objects. // // Compile this program with the following command line: // C:>csc Plus.cs // namespace nsOverload { using System; public class PlusclsMain { static public void Main () { clsPoint point1 = new clsPoint (12, 28, "This is part"); clsPoint point2 = new clsPoint (42, 64, " of a string"); clsPoint point3 = point1 + point2; Console.WriteLine ("Results for point3:"); Console.WriteLine (" Point is at " + point3); Console.WriteLine (" str = " + point3.str); } } class clsPoint { public clsPoint () { } public clsPoint (int x, int y, string str) { m_cx = x; m_cy = y; this.str = str; } private int m_cx = 0; private int m_cy = 0; public int cx { get {return (m_cx);} set {m_cx = value;} } public int cy { get {return (m_cy);} set {m_cy = value;} } public string str = ""; static public clsPoint operator +(clsPoint pt1, clsPoint pt2) { clsPoint point = new clsPoint(); point.cx = pt1.cx + pt2.cx; point.cy = pt1.cy + pt2.cy; point.str = pt1.str + pt2.str; return (point); } public override string ToString() { return ("(" + m_cx + "," + m_cy + ")"); } } }