c# 基础之方法

方法:方法其实就是函数(Function),优点就是方便重复使用,使代码简洁。

方法参数分:

1)值参数,不含修饰符

2)引用参数,以ref修饰符声明

3)输出参数,以out修饰符声明

4)数组型参数,以params声明

最长用的语法格式:

        //语法:[访问修饰符] 返回值 方法名(参数1,参数2)
        public static int NewMethod(int a,int b)
        {
            return a > b ? a : b;       //max
        }

1.  值传递:

     值传递是方法默认的参数类型,采用的是值复制的方式。把实参值复制给形参,所以这种参数传递方式的特点是形参的值发生改变时,不会影响实参的值,从而保障了实参数据的安全

 

 1         public static void ChangeValue(int x,int y)
 2         {
 3             int temp = x; //交换x y值
 4             x = y;
 5             y = temp;
 6         }
 7 
 8         static void Main(string[] args)
 9         {
10             int x = 1, y = 2;
11             ChangeValue(x, y);
12             Console.WriteLine("x = {0},y = {1}", x, y);
13             Console.ReadKey();
14             /*
15              * 输出结果为: x=1, y=2
16              * 方法内的x y放生了改变,但是没影响方法外的x y.
17              */
18         }
View Code

 

 2.引用传递分(ref和out)

首先:两者都是按地址传递的,使用后都将改变原来参数的数值。

 

其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空。这个就是两个的区别,或者说就像有的网友说的,ref是有进有出,out是只出不进。

所以:使用ref,实参必须是赋值后的变量。纯值 如:1、3、“2年级一班”的实参就会报错。

 

 1         public static int GetMaxMin(int[] arr, ref int min)
 2         {
 3             int max = arr[0];
 4             min = arr[0];       //ref 参数 在方法内可以不赋值    
 5             for (int i = 1; i < arr.Length; i++)
 6             {
 7                 if (max < arr[i])
 8                 {
 9                     max = arr[i];
10                 }
11                 if (min > arr[i])
12                 {
13                     min = arr[i];
14                 }
15             }
16             return max;
17         }
18 
19         static void Main(string[] args)
20         {
21             int min, max;
22             min = 0; //ref 实参变量使用前必须赋值,并且直接修改此值.
23             //定义随机数,遍历成数组
24             Random ro = new Random();
25             int[] myArray = new int[10];
26             for (int i = 0; i < myArray.Length; i++)
27             {
28                 myArray[i] = ro.Next(1, 100);
29             }
30             //遍历数组
31             for (int i = 0; i < myArray.Length; i++)
32             {
33                 Console.Write(myArray[i].ToString() + ",");
34             }
35             //调用方法返回最大值
36             max = GetMaxMin(myArray, ref min);
37             Console.WriteLine("方法返回值max = {0},ref参数修改后min = {1}", max, min);
38             Console.ReadKey();
39 
40         }
ref参数

 

 

 使用out,和ref差不多。区别out参数变量在方法外可以不赋值但是在方法内必须重新赋值。所以使用out是无法将实参的值带入到方法体内的。我估计作用:将方法体中的值带出方法外。

比如求数组中最大值和最小值的方法,事例如下:

 

 1         public static int GetMaxMin(int[] arr, out int min)
 2         {
 3             int max = arr[0];
 4             min = arr[0];       //out 在方法体内必须赋值.    
 5             for (int i = 1; i < arr.Length; i++)
 6             {
 7                 if (max < arr[i])
 8                 {
 9                     max = arr[i];
10                 }
11                 if (min > arr[i])
12                 {
13                     min = arr[i];
14                 }
15             }
16             return max;
17         }
18 
19 
20         static void Main(string[] args)
21         {
22             int min, max;//out 返回min可以不赋值
23             //定义随机数,遍历成数组
24             Random ro = new Random();
25             int[] myArray = new int[10];
26             for (int i = 0; i < myArray.Length; i++)
27             {
28                 myArray[i] = ro.Next(1, 100);
29             }
30             //遍历数组
31             for (int i = 0; i < myArray.Length; i++)
32             {
33                 Console.Write(myArray[i].ToString() + ",");
34             }
35             //调用方法返回最大值
36             max = GetMaxMin(myArray, out min);
37             Console.WriteLine("方法返回值max = {0},out参数返回值 min = {1}", max, min);
38             Console.ReadKey();
39 
40         }
out参数

 

4.   数组型参数类型(params类型)

params关键字可以指定在参数数目可变处采用参数的方法参数。也就是说。使用params可以自动把你传入的值按照规则转换为一个新建的数组。
在方法声明中的 params关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params关键字。

 

 1         public static int GetSum(ref int avg, params int[] total)
 2         {
 3             int sum = 0;
 4             for (int i = 0; i < total.Length; i++)
 5             {
 6                 sum += total[i];
 7             }
 8             avg = sum / total.Length;       //平均成绩
 9             return sum;
10         }
11 
12         static void Main(string[] args)
13         {
14             //定义各科变量
15             int chainese, english, math,total,avg;
16             avg =0;     //ref实参变量必须赋值
17             chainese = 88;
18             english = 90;
19             math = 62;
20             total = GetSum(ref avg, chainese, english, math);//最后的3个参数传入到total数组中了
21             Console.WriteLine("你的总成绩{0}分,平均成绩{1}",total,avg);
22             Console.ReadKey();
23             /*总成绩240,平均80
24              *              
25              */
26 
27         }
params,事例

 

 方法重载:1.方法名相同。2.方法参数个数或参数类型不同其中ref和out与无ref和out也能构成方法重载。

事例,通过2个整数相加、3个数相加、2个小数相加

 

 1         //x为两数中最大数
 2         public static int Add(ref int x ,int y) //参数int 2个 其中参数1添加ref
 3         {
 4             int temp;
 5             temp = x;
 6             x = x > y ? x : y;
 7             return temp + y;
 8         }
 9         public static int Add(int x,int y)  //参数int 2个
10         {
11             return x + y;
12         }
13 
14         public static int Add(int x, int y,int z)   //参数int 3个
15         {
16             return x + y+z;
17         }
18         public static double Add(double x, double y) //参数double 3个
19         {
20             return x + y;
21         }
22 
23 
24         static void Main(string[] args)
25         {
26             int sum1, sum2, refSum, max;
27             max = 10;
28             double sum3;
29             sum1 = Add(10, 20);
30             sum2 = Add(10, 20, 30);
31             sum3 = Add(10.5, 20.5);
32             refSum = Add(ref max, 20);
33             Console.WriteLine("sum1 ={0},sum2 ={1},sum3 ={2},refSum ={3},max = {4}", sum1, sum2, sum3, refSum, max);
34             Console.ReadKey();
35             /*max传入方法时值 10.方法结束后max = 20
36              * 返回值sum1 =30,sum2 =60,sum3 =31,refSum =30,max = 20
37              */
38         }
方法重载

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2017-07-11 22:57  世人迷茫  阅读(176)  评论(0编辑  收藏  举报