0% found this document useful (0 votes)
19 views10 pages

Method Overloading: Ali Shakir Alahmed

Uploaded by

Laween Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Method Overloading: Ali Shakir Alahmed

Uploaded by

Laween Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Duhok Polytechnic University Shekhan

Technical Institute Information


Technology Department

Method Overloading
Ali Shakir Alahmed

2022–2023
C# Method Overloading
• In C#, there might be two or more methods with the same name but
different numbers, types, and order of parameters, it is called
method overloading. For example:
void display() { ... }
void display(int a) { ... }
float display(double a) { ... }
float display(int a, float b) { ... }
C# Method Overloading
We can perform method overloading in the following
ways:
1. By
void changing
display(int the
a) { ... } Number of Parameters
void display(int a, int b) { ... }

• 2. By changing the Data types of the parameters


void display(int a) { ... }
void display(string b) { ... }

• 3.display(int
void By changing the
a, string Order
b) { ... } of the parameters
void display(string b, int a) { ... }
Method Overloading Example cont.

static int PlusMethod(int x, int y)


{
return x + y;
}

static double PlusMethod(double x, double y)


{
return x + y;
}
static void Main(string[] args)
{
int myNum1 = PlusMethod(8, 5);
double myNum2 = PlusMethod(4.3, 6.26);
Console.WriteLine("Int: " + myNum1);
Console.WriteLine("Double: " + myNum2);
}
Q- write a C# program to build function
1- sum (2) numbers
2- sum (3) numbers
class Method_overloading
static void Main(string[] args)
{
static int Addition(int a, int b) {
{ int x = Addition(4, 7);
int x;
int y = Addition(5, 6, 4);
return x = a + b;
} Console.WriteLine("The sum of t is = " + x);
static int Addition(int a, int b, int c) Console.WriteLine("The sum of t is = " + y);
{
int y; Console.ReadKey();
return y = a + b + c; }
}
class Program
{
static float Addition(float a, float b)
{
float u;
return u = a + b;
}
static float Addition(float a, float b, float c)
{
float v;
return v = a + b + c;
}
static void Main(string[] args)
{
Console.WriteLine("Addition of two integers:" + Addition(2, 5));
Console.WriteLine("Addition of two double type values:" + Addition(0.40f, 0.50f));
Console.WriteLine("Addition of three integers:" + Addition(2, 5, 5));
Console.WriteLine("Addition of three double type :" + Addition(0.40f, 0.50f, 0.60f));
Console.ReadLine();
}}}
Q: Write a program in C# language to read 10 numbers then sum (Even)
numbers by use the function?
public static int exam(int[]x) static void Main(string[] args)
{
{
int sum = 0; int[] a = new int[10];
for (int i = 0; i < 10; i++) Console.WriteLine("enter the number");
{
if (x[i] % 2 == 0) for (int i = 0; i < 10; i++)
{ {
sum = sum + x[i]; a[i] = int.Parse(Console.ReadLine());
} }
}
Console.WriteLine("\nThe Sum is of Even
return (sum); number : {0}\n", exam(a));
}
Console.ReadKey();
}}}
Use method to find an area of circle ?
Note : area =

static void area(double r , double pi)


{
double area = pi * r * r;
Console.WriteLine("the area is : "+area);
}
static void Main(string[] args)
{
const double pi = 3.14;
Console.Write("Enter The Radius of circle : ");
double r = double.Parse(Console.ReadLine());
area(r, pi);
Console.ReadKey();
}
Use method to input number and string and print it ?
class Program static void Main(string[] args)
{ {
// method with int and string parameters Program p1 = new Program();
void display(int a, string b) p1.display(100, "Programming");
{
p1.display("Programiz", 400);
Console.WriteLine("int: " + a);
Console.WriteLine("string: " + b);
}
// method with string and int parameter
void display(string b, int a)
{
Console.WriteLine("string: " + b);
Console.WriteLine("int: " + a);
}

You might also like