Illustrates method overloading : Overloading Method « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Overloading MethodScreenshots 
Illustrates method overloading
Illustrates method overloading

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_9.cs illustrates method overloading
*/


// declare the Swapper class
class Swapper
{

  // this Swap() method swaps two int parameters
  public void Swap(ref int x, ref int y)
  {
    int temp = x;
    x = y;
    y = temp;
  }

  // this Swap() method swaps two float parameters
  public void Swap(ref float x, ref float y)
  {
    float temp = x;
    x = y;
    y = temp;
  }

}


public class Example5_9
{

  public static void Main()
  {

    // create a Swapper object
    Swapper mySwapper = new Swapper();

    // declare two int variables
    int intValue1 = 2;
    int intValue2 = 5;
    System.Console.WriteLine("initial intValue1 = " + intValue1 +
      ", intValue2 = " + intValue2);

    // swap the two float variables
    // (uses the Swap() method that accepts int parameters)
    mySwapper.Swap(ref intValue1, ref intValue2);

    // display the final values
    System.Console.WriteLine("final   intValue1 = " + intValue1 +
      ", intValue2 = " + intValue2);

    // declare two float variables
    float floatValue1 = 2f;
    float floatValue2 = 5f;
    System.Console.WriteLine("initial floatValue1 = " + floatValue1 +
      ", floatValue2 = " + floatValue2);

    // swap the two float variables
    // (uses the Swap() method that accepts float parameters)
    mySwapper.Swap(ref floatValue1, ref floatValue2);

    // display the final values
    System.Console.WriteLine("final   floatValue1 = " + floatValue1 +
      ", floatValue2 = " + floatValue2);
    mySwapper.Swap(ref floatValue1, ref floatValue2);

  }

}

           
       
Related examples in the same category
1.Overloaded methods with identical signatures cause compilation errors, even if return types are different.
2.Operator Overloading
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.