Lecture overview -- Keyboard shortcut: 'u'  Previous page: An example of overloaded operators: Interval -- Keyboard shortcut: 'p'  Next page: Delegates [Section] -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 31 : 45
Object-oriented Programming in C#
Data Access and Operations
Some details of operator overloading

Additional details about operator overloading. Not the full story, however.


public static return-type operator symbol(formal-par-list){
  body-of-operator
}

The C# syntax for definition of an overloaded operator.

  • Operators must be public and static

  • One or two formal parameters must occur, corresponding to unary and binary operators

  • At least one of the parameters must be of the type to which the operator belongs

  • Only value parameters apply

  • Some operators must be defined in pairs (either none or both):

    • ==   and   !=            <   and   >            <=   and   >=

  • The special unary boolean operators true and false define when an object is playing the role as true or false in relation to the conditional logical operators

  • Overloading the binary operator op causes automatic overloading of the assignment operator op=

The list of rules above is not exhaustive