.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Illustrates method overloading

Posted By:SanthoshNathan       Posted Date: November 02, 2010    Points: 15    Category: Visual Studio    URL: http://www.dotnetspark.com  

Illustrates method overloading
 

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);

  }

}

           


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend