Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to use methods overloading in C#

7 Answers

0 votes
using System;
 
class Program
{
    static void Method(int n) {
        Console.WriteLine(n);
    }
    
    static void Method(string s) {
        Console.WriteLine(s);
    }

    static void Main(string[] args)
    {
        try {
            Method("c#");
 
            Method(100);
        }
        
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}


 
/*
run:
 
c#
100
    
*/

 



answered Mar 13, 2017 by avibootz
edited Dec 18, 2023 by avibootz
0 votes
using System;
 
class Program
{
    static int Method(int n) {
        return n;
    }
    
    static string Method(string s) {
        return s;
    }

    static void Main(string[] args)
    {
        try {
            Console.WriteLine(Method("c#"));
 
            Console.WriteLine(Method(100));
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}



 
/*
run:
 
c#
100
    
*/

 



answered Mar 13, 2017 by avibootz
edited Dec 18, 2023 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Method(10, 20);

                Method(3.14, 60);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Method(int x, int y)
        {
            Console.WriteLine("{0}, {1}", x, y);
        }
        static void Method(double x, int y)
        {
            Console.WriteLine("{0}, {1}", x, y);
        }
    }
}

/*
run:

10, 20
3.14, 60
   
*/

 



answered Mar 13, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Parent
    {
        public void m(int x)
        {
            Console.WriteLine(x);
        }
    }
    class Child : Parent
    {
        public void m(double y)
        {
            Console.WriteLine(y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Child o = new Child();

            o.m(10);

            o.m(3.14);
        }
    }
}

/*
run:

10
3.14

*/

 



answered Mar 13, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Method(10);

                Method(3.14, 60);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Method(int x)
        {
            Console.WriteLine("1. {0}", x);
        }
        static void Method(int x, int y = 20)
        {
            Console.WriteLine("2. {0}, {1}", x, y);
        }
        static void Method(double x, int y)
        {
            Console.WriteLine("3. {0}, {1}", x, y);
        }
    }
}

/*
run:
 
1. 10
3. 3.14, 60
    
*/

 



answered Mar 13, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Method(10, 13, 18);

                Method(80, 60);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        static void Method(int x, int y = 20)
        {
            Console.WriteLine("1. {0}, {1}", x, y);
        }
        static void Method(int x, int y = 20, int z = 30)
        {
            Console.WriteLine("2. {0}, {1}, {2}", x, y, z);
        }
    }
}

/*
run:
 
2. 10, 13, 18
1. 80, 60
    
*/

 



answered Mar 13, 2017 by avibootz
0 votes
using System;
 
 
public static class Module1
{
    class Sample {
        public void Add(int num1, int num2) {
            int sum = 0;
 
            sum = num1 + num2;
             
            Console.WriteLine("Sum = {0}", sum);
        }
 
        public void Add(int num1, int num2, int num3) {
            int sum = 0;
 
            sum = num1 + num2 + num3;
             
            Console.WriteLine("Sum = {0}", sum);
        }
    }
    public static void Main()
    {
        Sample obj = new Sample();
 
        obj.Add(4, 8);
        obj.Add(9, 2, 5);
    }
}
 
 
 
 
/*
run:
 
Sum = 12
Sum = 16
 
*/

 



answered Dec 18, 2023 by avibootz

Related questions

1 answer 238 views
238 views asked Sep 6, 2015 by avibootz
1 answer 210 views
2 answers 228 views
1 answer 190 views
1 answer 160 views
1 answer 140 views
140 views asked Jan 1, 2017 by avibootz
...