C#
  Home arrow C# arrow Page 4 - 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 
Mobile Linux 
App Generation ROI 
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 - Overload the other way


    (Page 4 of 5 )

    With the same example, replace the Main method with the following updated code and try to compile the application.

    static void Main(string[] args)
    {
    Number i = new Number();
    i.aNumber = 90;
    i.aDecimal = .5m;
    int result = 9 + i;
    Console.WriteLine("the value of i.aNumber = {0}", i.aNumber);
    Console.WriteLine("i.ToString() returns {0}", i.ToString());
    Console.WriteLine("result = {0}", result);
    Console.ReadLine();
    }

    This code will not compile, and it will generate the following error "Operator '+' cannot be applied to operands of type 'int' and 'Operators.Number.'" What happened? We have written an expression that consists of a constant field of type int, then the + operator, followed by an instance of type Number, so what's wrong?

    Actually the operator overload method we wrote has a signature that accepts the Number instance first, then the int variable. As you know, the parameters' order is very important, so the compiler has generated an error. To solve this problem we need to write another method that accepts an int parameter followed by the Number type parameter.

    public static int operator+(int x, Number num)
    {
    return num.aNumber + x;
    }

    and this is the complete code example

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Number i = new Number();
    i.aNumber = 90;
    i.aDecimal = .5m;
    int result = 9 + i;
    Console.WriteLine("the value of i.aNumber = {0}", i.aNumber);
    Console.WriteLine("i.ToString() returns {0}", i.ToString());
    Console.WriteLine("result = {0}", result);
    Console.ReadLine();
    }
    }

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

    public static int operator+(Number num, int x)
    {
    return num.aNumber + x;
    }

    public static int operator+(int x, Number num)
    {
    return num.aNumber + x;
    }

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

    }
    }
     

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - 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...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT