We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rtipton on 11/27/09


Tagged

method Overloading


Versions (?)


Method Overloading


Published in: C# 


To accomplish Method Overloading, a developer can define two or more methods with the same name. Each method will take a different set of parameters. The parameter combination or signature, is what the compiler uses to determine which method to use.

  1. using System;
  2. namespace MethodOverloading
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. MethodOverloadPlay(10, 3);
  9. Console.WriteLine("+++++");
  10. MethodOverloadPlay(5, 5, 6);
  11. Console.WriteLine("+++++");
  12. MethodOverloadPlay("SP");
  13. Console.ReadLine();
  14. }
  15.  
  16. static void MethodOverloadPlay(int number1, int number2)
  17. {
  18. int result = number1 + number2;
  19. Console.WriteLine(result);
  20. }
  21.  
  22. static void MethodOverloadPlay(int number1, int number2, int number3)
  23. {
  24. int result = number1 + number2 + number3;
  25. Console.WriteLine(result);
  26. }
  27.  
  28. static void MethodOverloadPlay(string string1)
  29. {
  30. Console.WriteLine("Breaking Benjamin & Sick Puppies Rock!");
  31. }
  32. }
  33. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: victorboba on December 4, 2009

Another way that ensures there's only one code-base method that implements the business functionality is to call the overloaded methods in sequence:

in the first MethodOverloadPlay you would call:

int result = MethodOverloadPlay(number1, number2, 0);

You need to login to post a comment.