C#
  Home arrow C# arrow Page 5 - Behind the Scenes Look at C#: Operators, 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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#

Behind the Scenes Look at C#: Operators, continued
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2005-07-06

    Table of Contents:
  • Behind the Scenes Look at C#: Operators, continued
  • Operator overloading, first visit
  • Can we do that?
  • Overload the other way
  • Operator overloading, revisited

  • 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


    Behind the Scenes Look at C#: Operators, continued - Operator overloading, revisited


    (Page 5 of 5 )

    In our example it makes sense to use the addition operator + with two instances of the Number class, so we need to overload the + operator to do that.

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Number i = new Number();
    i.aNumber = 90;
    i.aDecimal = .5m;

    Number o = new Number();
    o.aNumber = 10;
    o.aDecimal = .3m;

    Number SumNumber = i + o;
    Console.WriteLine("The value of i + o = {0}", SumNumber.aNumber);
    Console.ReadLine();
    }
    }

    public class Number
    {
    public int aNumber;
    public decimal aDecimal;

    public static Number operator+(Number num, Number num2)
    {
    Number temp = new Number();
    temp.aNumber = num.aNumber + num2.aNumber;
    return temp;
    }

    public override string ToString()
    {
    return Convert.ToString(this.aNumber + this.aDecimal);
    }

    }
    }

    The result will be:

    The ability to express basic operations on user-defined types is very unlimited and complex. Operator overloading is one feature that C# has over VB.NET. Usually when you overload an operator you will overload its family. In other words, when you overload the addition operator + you will need to overload all of the rest of the arithmetic operators (*, /, - and %). The compiler will not issue a warning if you overload only one of these operators, but it does make sense to overload all of them.

    Why can you add a Number instance to another one, but you can't subtract them? If you have overloaded the > operator, it makes sense to overload the < operator, and it will be perfect if you overload the >= and the <= operators. The C# compiler will not let you overload only one operator without overloading the other one (this is different from the arithmetic operators). Let's complete our example by overloading the whole arithmetic operators family.

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Number i = new Number();
    i.aNumber = 90;
    i.aDecimal = .5m;

    Number o = new Number();
    o.aNumber = 10;
    o.aDecimal = .3m;

    Number SumNumber = i + o;
    Console.WriteLine("The value of i + o = {0}", SumNumber.aNumber);
    SumNumber = i - o;
    Console.WriteLine("The value of i - o = {0}", SumNumber.aNumber);
    SumNumber = i * o;
    Console.WriteLine("The value of i * o = {0}", SumNumber.aNumber);
    SumNumber = i / o;
    Console.WriteLine("The value of i / o = {0}", SumNumber.aNumber);
    SumNumber = i % o;
    Console.WriteLine("The value of i % o = {0}", SumNumber.aNumber);
    Console.ReadLine();
    }
    }

    public class Number
    {
    public int aNumber;
    public decimal aDecimal;

    public static Number operator+(Number num, Number num2)
    {
    Number temp = new Number();
    temp.aNumber = num.aNumber + num2.aNumber;
    return temp;
    }

    public static Number operator-(Number num, Number num2)
    {
    Number temp = new Number();
    temp.aNumber = num.aNumber - num2.aNumber;
    return temp;
    }

    public static Number operator*(Number num, Number num2)
    {
    Number temp = new Number();
    temp.aNumber = num.aNumber * num2.aNumber;
    return temp;
    }

    public static Number operator/(Number num, Number num2)
    {
    Number temp = new Number();
    temp.aNumber = num.aNumber / num2.aNumber;
    return temp;
    }

    public static Number operator%(Number num, Number num2)
    {
    Number temp = new Number();
    temp.aNumber = num.aNumber % num2.aNumber;
    return temp;
    }

    public override string ToString()
    {
    return Convert.ToString(this.aNumber + this.aDecimal);
    }

    }
    }

    Compile and run the code to get the following result to the console window:

     


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway