c#第七天
属性
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
一般情况下,字段都会被设置为private,通过属性访问
引用参数 ref
public void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
myMath.Swap(ref score_1, ref score_2);
输出参数 out
public void Cal(int a, int b, out int x, out int y)
{
x = a - b;
y = a + b;
}
myMath.Cal(numOne, numTwo, out resultOne, out resultTwo);
数组参数 params
public void Sum(params int[] a)
{
int result = 0;
foreach (int x in a)
{
result += x;
}
Console.WriteLine("结果为: " + result);
}
myMath.Sum(1, 2, 3, 4, 5);