85 lines
1.3 KiB
Markdown
85 lines
1.3 KiB
Markdown
# Methods
|
|
|
|
```C#
|
|
singHappyBirthday();
|
|
|
|
static void singHappyBirthday(){
|
|
|
|
Console.WriteLine("Happy birthday to you")
|
|
}
|
|
```
|
|
|
|
## arguments
|
|
|
|
```C#
|
|
String name = "mrsh"
|
|
int age = 30;
|
|
|
|
singHappyBirthday(name, age);
|
|
|
|
static void singHappyBirthday(String namem int age){
|
|
|
|
Console.WriteLine("Happy birthday to you" + name)
|
|
Console.WriteLine("you are " + age + "years old")
|
|
}
|
|
```
|
|
|
|
## return
|
|
|
|
```C#
|
|
double x = 5.3;
|
|
double y = 3;
|
|
|
|
double result = Multiply(x,y)
|
|
|
|
static double Multiply(double x, double y){
|
|
double z = x * y;
|
|
return z;
|
|
}
|
|
```
|
|
|
|
## method overloading
|
|
|
|
```C#
|
|
double x = 5.3;
|
|
double y = 3;
|
|
double z = 3;
|
|
|
|
double result = Multiply(x,y)
|
|
// Will return the first Multiply method
|
|
double result = Multiply(x,y,z)
|
|
// Will return the second Multiply method
|
|
|
|
static double Multiply(double x, double y){
|
|
double z = x * y;
|
|
return z;
|
|
}
|
|
|
|
static double Multiply(double a, double b, double c){
|
|
double z = x * y;
|
|
return z;
|
|
}
|
|
```
|
|
|
|
## params
|
|
|
|
params keyword a method parameter that takes a variale number of arguments.
|
|
the parameter type must be a single - dimensional array
|
|
|
|
```C#
|
|
|
|
double total = CheckOut(3.99, 5,75, 15);
|
|
|
|
static double Checkout(params double[] prices){
|
|
|
|
double total =0;
|
|
foreach(double price in prices)
|
|
{
|
|
total += prices;
|
|
}
|
|
|
|
return total;
|
|
}
|
|
```
|
|
|