C#
  Home arrow C# arrow Page 2 - Overriding versus Overloading
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#

Overriding versus Overloading
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-10

    Table of Contents:
  • Overriding versus Overloading
  • Method Overloading Example
  • Overload a Constructor
  • Overriding

  • 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


    Overriding versus Overloading - Method Overloading Example


    (Page 2 of 4 )

    Here is a full example that illustrates method overloading:


    namespace OverloadingMethods

    {

    class   Program

    {

    static   void Main( string [] args)

    {

    Point p1 = new   Point ();

    Console .WriteLine( "p1 -> {0}" , p1);

    p1.Move();

    Console .WriteLine( "p1 -> {0}" , p1);

    p1.Move(2);

    Console .WriteLine( "p1 -> {0}" , p1);

    p1.Move(4, 7);

    Console .WriteLine( "p1 -> {0}" , p1);


    Point p2 = new   Point (2, 5);

    Console .WriteLine( "p2 -> {0}" , p2);

    p2.Move(10, 9);

    Console .WriteLine( "p2 -> {0}" , p2);

    p2.Move();

    Console .WriteLine( "p2 -> {0}" , p2);

    }

    }


    class   Point

    {

    private   int x;

    private   int y;

    Random rand = new   Random ();


    public Point()

    {

    x = y = 0;

    }


    public Point( int x_value, int y_value)

    {

    x = x_value;

    y = y_value;

    }


    //Move x with a random number between 1 and 100 (inclusive)

    //Move y with a random number between 1 and 50 (inclusive)

    public   void Move()

    {

    x += rand.Next(1, 101);

    y += rand.Next(1, 51);

    }


    //move x and y by the same number (delta)

    public   void Move( int delta)

    {

    x += delta;

    y += delta;

    }


    //move x and y by the specified values

    public   void Move( int delta_x, int delta_y)

    {

    x += delta_x;

    y += delta_y;

    }


    public   override   string ToString()

    {

    return   string .Format( "[{0},{1}]" , x, y);

    }

    }

    }


    Notice that the Point class is flexible in the way a point can be moved. By overloading the Move() method, we are able to move a point via different x and y values, via the same value (delta), or via random x and y values. This way, our class gives its user different ways of moving a point using the same friendly Move() method.

    More C# Articles
    More By Ayad Boudiab


       · Welcome to my latest article on overriding versus overloading.I am looking forward...
     

    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 1 hosted by Hostway
    Stay green...Green IT