Overloading in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjitsarma
    New Member
    • Jul 2007
    • 34

    Overloading in c#

    Can we use overload keyword in overloading operation in C# ?

    In the below example 'overloading is done'.If the same example is used in vb.net,'overloa d' keyword is used.I want to know can we use 'overload' keyword here and does c# use 'overload' keyword ?

    public class class1
    {
    public void area(int l, int b)
    {
    int ar;
    ar = (l * b);
    MessageBox.Show ("area of rectangle=" + ar);
    }

    public void area(int a)
    {
    int ar;
    ar = (a * a);
    MessageBox.Show ("area of square=" + ar);
    }

    }

    private void button1_Click(o bject sender, EventArgs e)
    {
    class1 c1 = new class1();
    c1.area(3, 4);
    c1.area(4);

    }
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by manjitsarma
    Can we use overload keyword in overloading operation in C# ?

    In the below example 'overloading is done'.If the same example is used in vb.net,'overloa d' keyword is used.I want to know can we use 'overload' keyword here and does c# use 'overload' keyword ?

    public class class1
    {
    public void area(int l, int b)
    {
    int ar;
    ar = (l * b);
    MessageBox.Show ("area of rectangle=" + ar);
    }

    public void area(int a)
    {
    int ar;
    ar = (a * a);
    MessageBox.Show ("area of square=" + ar);
    }

    }

    private void button1_Click(o bject sender, EventArgs e)
    {
    class1 c1 = new class1();
    c1.area(3, 4);
    c1.area(4);

    }
    only the difference is there is an extra "s" ...........
    The Overloads keyword is used in VB.NET
    while the Overload keyword is used in C# (There is no other differences)

    Comment

    • manjitsarma
      New Member
      • Jul 2007
      • 34

      #3
      Ok.In this example I tried to use 'overload' keyword in 'public void area(int l, int b)'.But when I typed it is showing 'overloading' keyword only.Could you tell me where to use 'overload' keyword in this program ?

      Regards...

      Manjit Sarma

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        In C# (and I had thought in VB.NET, but maybe not)
        The normal thought on function overloading is done implicitly without the overload keyword.

        void area(int a)
        and
        void area(int a, int b)

        Don't need anything else with them.

        In c# the OVERLOAD keyword is used when you wish to overload a function already implemented in a base class. The most commonly overloaded function is the ToString()

        If I make my own class, the default .ToString() function will just return the name of my class
        Code:
        public class myclass
        {
           public myclass()
           {//my generic constructor
           }
        }
        But I can overload it with:
        Code:
        public class myclass
        {
           public myclass()
           {//my generic constructor
           }
           public overload string ToString()
           {//my custom .ToString() function
              return "This is my ToString()";
           }
        }

        Comment

        • manjitsarma
          New Member
          • Jul 2007
          • 34

          #5
          Thank you very much for clearing my doubt.I will work on 'Overloading' examples now.

          Regards...

          Manjit Sarma

          Comment

          Working...