C#
  Home arrow C# arrow Page 2 - Overloading Operators in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Overloading Operators in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-10-30

    Table of Contents:
  • Overloading Operators in C#
  • Operator Overloading
  • Overloading Equality and Comparison Operators
  • Overloading true and false

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Overloading Operators in C# - Operator Overloading


    (Page 2 of 4 )

    Overview

    Operators can be overloaded to provide more natural syntax for custom types. Operator overloading is most appropriately used for implementing custom structs that represent fairly primitive data types. For example, a custom numeric type is an excellent candidate for operator overloading.

    Table 4-1 lists the overloadable symbolic operators.

    Table 4-1. Overloadable symbolic operators

    + (unary)

    - (unary)

    !

    ~

    ++

    --

    +

    -

    *

    /

    %

    &

    |

    ^

    <<

    >>

    ==

    !=

    >

    <

    >=

    <=

     

     

     

    The following operators are also overloadable:

    1. Implicit and explicit conversions (with theimplicitandexplicitkeywords)

    2. The literalstrueandfalse

    The following operators are indirectly overloaded:

    1. The compound assignment operators (e.g.,+=,/=) are implicitly overridden by overriding the noncompound operators (e.g.,+,=).

    2. The conditional operators&&and||are implicitly overridden by overriding the bitwise operators&and|.

    Operator Functions

    An operator is overloaded by declaring an operator function. An operator function has the following rules:

    1. The name of the function is specified with theoperatorkeyword followed by an operator symbol.
    2. The operator function must be markedstatic.

    3. The parameters of the operator function represent the operands.

    4. The return type of an operator function represents the result of an expression.

    5. At least one of the operands must be the type in which the operator function is declared.

    In the following example, we define a struct calledNoterepresenting a musical note, and then overload the+operator:

      public struct Note
      {
        int value;
        public Note (int semitonesFromA) { value = semitonesFromA; }

        public static Note operator + (Note x, int semitones)
       
    {
           return new Note (x.value + semitones);
        }
      }

    This overload allows us to add anintto aNote:

      Note B = new Note(2);
      Note CSharp = B + 2;

    Overloading an assignment operator automatically supports the corresponding compound assignment operator. In our example, since we overrode+, we can use+=too:

      CSharp += 2;

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek