C#
  Home arrow C# arrow Page 3 - 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 - Can we do that?


    (Page 3 of 5 )

    I will modify the example to return an int value (not an object of type Number) to prove that we have control over the return type.

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    Number i = new Number();
    i.aNumber = 90;
    i.aDecimal = .5m;
    int result = i + 9;
    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 override string ToString()
    {
    return Convert.ToString(this.aNumber + this.aDecimal);
    }

    }
    }

    Compile and run the program and you will get the following result to the console window.

    Look at the Main method and you will find that we are able to return a value of type int from the expression "int result = i + 9". This happened because we have modified the operator overload method to return an int value through two steps. First we have replaced the return data type Number by an int, then inside the method we have added the num.aNumber to x, and this is the value that will be returned.

    More C# Articles
    More By Michael Youssef


     

    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 3 hosted by Hostway