params ref和out 枚举 面向对象
1,params的用法
2,ref和out在传址调用中的好处
3,枚举和“装箱,拆箱”
4,通讯录与面向对象
5,命名空间
Params测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 上午
{
class Params测试
{
static void AddEmp(params double[] salary)
{
for (int i = 0; i <salary.Length; i++)
{
salary[i] *= 1.2;
}
}
static void Main1()
{
double[] salary = { 3400, 1700, 2500 };
AddEmp(salary);
for (int i = 0; i <salary.Length; i++)
{
Console.WriteLine(salary[i]);
}
double a = 2300, b = 4600;
AddEmp(a,b);
Console.WriteLine(a+","+b);
string str = "a,b.c d";
string[] strs=str.Split(',', '.', ' ', ';');
PrintStringArrays(strs);
}
private static void PrintStringArrays(string[] strs)
{
for (int i = 0; i < strs.Length; i++)
{
Console.WriteLine(strs[i]);
}
}
}
}
函数refout
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 上午
{
class 函数refout
{
static void AddEmp(ref double salary)
{
salary *= 1.2;
}
static void SumAvg(double x,double y,out double sum,out double avg)
{
sum = x + y;
avg = sum / 2;
}
static void Main1()
{
double salary=4500;
AddEmp(ref salary);//传的不再是数值 而是地址 意味着在被掉函数中会改变主调函数的实参值
Console.WriteLine(salary);
double a, b;
SumAvg(1, 2, out a,out b);
Console.WriteLine(a+","+b);
}
}
}
函数的参数传递
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 上午
{
class Emp
{
public double salary;
}
class 函数的参数传递
{
static void AddEmp(double salary)
{
salary *= 1.2;
Console.WriteLine("addEmp salary="+salary);
}
static void AddEmp(Emp emp)
{
emp.salary *= 1.2;
}
static void Main1()
{
//double[] salarys = {3400,7800,4500};
//for (int i = 0; i < salarys.Length; i++)
//{
// AddEmp(salarys[i]);//传数值调用
//}
//for (int i = 0; i < salarys.Length; i++)
//{
// Console.WriteLine(salarys[i]);
//}
Emp emp = new Emp();
emp.salary = 4500;
AddEmp(emp);//传地址调用
Console.WriteLine(emp.salary);
//传地址调用在被调函数中会改变主调函数的实参值 new出来的变量 对象 数组
//传数值调用在被调函数中不会改变主调函数的实参值 不是new出来的变量
}
}
}
函数定义
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 上午
{
class 函数定义
{
//XxxYyyZzz
static int JieCheng(int n)
{
int sum=1;
for (int i = 1; i <=n ; i++)
{
sum *= i;
}
return sum;
}
static void Main1()
{
Console.WriteLine("here1");
//在任何需要改返回值的地方都可以直接调用该函数以使用该函数的返回值
int x=JieCheng(5);
Console.WriteLine("5的阶乘为{0}",JieCheng(5));
Console.WriteLine("here2");
int sum = 1 + JieCheng(1+2) + JieCheng(5);
Console.WriteLine(sum);
}
}
}
枚举测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 上午
{
enum Operation
{
DoNothing,Exit,Dispose
}
enum Sex
{
男,女
}
class 枚举测试
{
public static void SelectBySex(Sex sex)
{
Console.WriteLine((int)sex);
Console.WriteLine(sex.ToString());
if (sex == Sex.男)
{
}
else if ( sex==Sex.女)
{
}
}
public static void F1(Operation op)
{
if(op==Operation.DoNothing)
{
Console.WriteLine("donothing操作");
}
else if(op==Operation.Dispose)
{
Console.WriteLine("dispose操作");
}
else if(op==Operation.Exit)
{
Console.WriteLine("exit操作");
}
}
static void Main1()
{
F1(Operation.DoNothing);
F1(Operation.DoNothing);
SelectBySex(Sex.男);
SelectBySex(Sex.女);
}
}
}
装箱拆箱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 上午
{
class 装箱拆箱
{
static void Main()
{
string str = 123;
object obj = 4500.5;
double d = (double)obj * 1.2;
Console.WriteLine(d);
}
}
}