C#
  Home arrow C# arrow Page 6 - C# Methods, part 1
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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#

C# Methods, part 1
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 35
    2005-04-26

    Table of Contents:
  • C# Methods, part 1
  • Method Signature
  • Instance Methods
  • Static Methods
  • Methods and StackFrame
  • Methods Overloading

  • 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


    C# Methods, part 1 - Methods Overloading


    (Page 6 of 6 )

    Method overloading is an Object oriented Design Principle. Sometimes you will have a behavior that you need to encapsulate inside a method that takes two parameters of type integer, and at another point in your application you need another method to concatenate two strings, and it's nice to name it. Ahh, but how?

    In Object Oriented Programming, you can overload the method with another variation, which means that you will provide another method (normally with another implementation) with the same name, but it MUST differ in the number of the parameters, or it can differ in the parameter's data type, or maybe the parameter's order, so it's a way for the C# Compiler to differentiate between the method variations.

    You can't use the method return type to make a new variation of the method, and you also can't overload a method with a property, because they are two different class members. When you use Visual Studio.NET, you can see how its intelliSense feature displays the method overloads; it will display the current method variation's parameter list with a number to indicate how many variations of this method exist.

    The following screen shot illustrates the Visual Studio.NET intelliSense feature of the method Console.WriteLine()

    There are 19 variations of the method WriteLine and, as you can see, there are arrows so that you can navigate through the variations and learn more about them. Also, there is a description for each parameter along with its data type. So let's overload the Add method.

    using System;

    namespace Methods
    {
    class Class1
    {

    static void Main(string[] args)
    {
    Console.WriteLine(Number.Add(4,5));
    Console.WriteLine(Number.Add(5,6,7));
    Console.WriteLine(Number.Add(1d,3d));
    Console.WriteLine(Number.Add(2d,3d,6d));
    Console.WriteLine(Number.Add("Hello","Overloading"));

    Console.ReadLine();
    }
    }

    public class Number
    {
    public static int Add(int x, int y)
    {
    return x + y;
    }

    public static int Add(int x, int y,int z)
    {
    return x + y + z;
    }

    public static double Add(double x, double y)
    {
    return x + y;
    }

    public static double Add(double x, double y, double z)
    {
    return x + y + z;
    }

    public static string Add(string x, string y)
    {
    return x +" "+ y;
    }
    }
    }

     
    The result is:

    As you can see, the code has been compiled, and we have five different versions of the Add method that differ in the parameter's data type, and some of them in the parameter's number. Note the use of the d letter after the value to denote a double literal value, so it can be differentiated from integer values, and so on.

    In the second part, after we discuss passing parameters by value and by reference, we will see that ref and out keyword can't overload a method.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT