-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorial 4
120 lines (102 loc) · 3.42 KB
/
Tutorial 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace tutorial_4
{
class Program
{
static void Main(string[] args)
{
//1.using values
double x = 5;
double y = 4;
Console.WriteLine("5+4={0}", GetSum(x, y));
Console.WriteLine("x{0}", x);
//2.using mathod and variable
int solution;
DoubleIt(15, out solution);//create a method called Double It, pass in 15, store it in solution
Console.WriteLine("15*2={0}", solution);
//3.using reference,pass by reference
int num1 = 10;
int num2 = 20;
Console.WriteLine("Before Swap num1:{0} num2:{1}", num1, num2);
Swap(ref num1, ref num2);
Console.WriteLine("After Swap num1:{0} num2:{1}", num1, num2);
/*4.parameter, By using the params keyword,
* you can specify a method parameter that takes a variable number of arguments.*/
Console.WriteLine("1+2+3={0}", GetSumMore(1, 2, 3));
//5. If you are using parameter, you can input the date in different sequence,
//as long as you give them names
PrintInfo(zipCode: 15147, name: "Leilei Xia");
//6.A function with two data input way
Console.WriteLine("5+4={0}", GetAnotherSum("5", "4.5"));
//7.enum, using symbolic name to represent data
//感觉这个真的超好用的!!
CarColor car1 = CarColor.Blue;
PaintCar(car1);
Console.ReadLine();
}
//*2.
static void DoubleIt(int x, out int solution)
{
solution = x * 2;
}
//1.*
static double GetSum(double x = 1, double y = 1)//you can define a value in method itself
{
double temp = x;
x = y;
y = temp;
return x + y;
}
//3.*
public static void Swap(ref int num1,ref int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
//4*
static double GetSumMore(params double[] nums)
{
double sum = 0;
foreach (int i in nums)//it's important to use foreach so that we can input multiple numbers
{
sum += i;//sum=sum+i
}
return sum;
}
//5.*
static void PrintInfo(string name, int zipCode)
{
Console.WriteLine("{0} lives in the zip code{1}", name, zipCode);
}
//6.*
static double GetAnotherSum(double x=1, double y = 1)
{
return x + y;
}
static double GetAnotherSum(string x = "1", string y = "1")//remember to change the parameter as well
//even the name of the functions are the same, we use method overloading, it's gonna call the right one
{
double dblX = Convert.ToDouble(x);
double dblY = Convert.ToDouble(y);
return dblX + dblY;
}
//7.*
enum CarColor : byte
{
Orange = 1,
Blue,
Green,
Red,
Yellow
}
static void PaintCar(CarColor cc)
{
Console.WriteLine("The car was painted {0} with the code{1}", cc,(int)cc);
}
}
}