C#参数修饰符总结

参数修饰符

参数修饰符

作用

无参数的情况默认为按值传递(pass by value),方法收到原始数据的副本

Out

输出参数由被调用方法赋值(按引用传递,pass by reference),如果方法没有对out参数赋值,将产生编译错误

Params

将一组可变数量的参数作为单独的逻辑参数进行传递,方法只能用一个params修饰符,而且必须是最后一个

Ref

调用者赋初值,并且可以由被调用的方法重新赋值(pass by reference),如果调用方法没有对ref参数赋值,不产生编译错误

 

1.默认无修饰符的参数按值传递

   按值传递,产生副本,原值不发生变化

代码
   
   
1 static int count( int x, int y)
2 {
3 int c = x + y;
4
5 x = 100 ; y = 200 ;
6
7 return c;
8 }
9
10 static void Main( string [] args)
11 {
12 Console.WriteLine( " *************************** " );
13
14 // pass by value
15  
16 int a = 1 , b = 2 ;
17
18 Console.WriteLine( " one,a:{0},b:{1} " , a, b);
19
20 Console.WriteLine( " two,count:{0} " ,count(a,b));
21
22 Console.WriteLine( " three,a:{0},b:{1} " , a, b);
23
24 Console.ReadLine();
25
26 }

结果:

***************************
one,a:1,b:2
two,count:3
three,a:1,b:2

 

2.out修饰符,引用传递(pass by reference)

  按引用传递,通过out可以得到多个返回值

 

代码
   
   
1      static void count( int x, int y, out int z)
2 {
3 z = x + y;
4 }
5
6
7
8
9 static void Main( string [] args)
10 {
11 Console.WriteLine( " 按引用传递,out修饰符*************************** " );
12
13 int a1 = 1 , b1 = 2 ;
14
15 // 不需要赋值,因为肯定会改变
16   int c;
17
18 count(a1, b1, out c);
19
20 Console.WriteLine( " c:{0} " , c);
21
22 Console.ReadLine();
23          }

结果:

 按引用传递,out修饰符***************************

 c:3

 

3.ref修饰符,按引用传递,改变原有指

  ref和out的区别:1)out参数不要进行初始化,因为方法必须赋值。2)ref类型,在引用前初始化

 

 

代码
   
   
1      static int count( ref int x, ref int y)
2 {
3 int c = x + y;
4
5 x = 100 ; y = 200 ;
6
7 return c;
8 }
9
10
11
12 static void Main( string [] args)
13 {
14
15
16 Console.WriteLine( " 按引用传递,ref参数*************************** " );
17
18 // pass by value
19  
20 int a2 = 1 , b2 = 2 ;
21
22 Console.WriteLine( " one,a:{0},b:{1} " , a2, b2);
23
24 Console.WriteLine( " two,count:{0} " , count( ref a2, ref b2));
25
26 Console.WriteLine( " three,a:{0},b:{1} " , a2, b2);
27
28 Console.ReadLine();
29        }

 

 

 结果:

 按引用传递,ref参数***************************

one,a:1,b:2

two,count:3 

three,a:100,b:200

 

4.params参数

1)引用类型按值传递

 

代码
   
   
1
2 static int count( params int [] arr)
3 {
4 int c = 0 ;
5 if (arr.Length == 0 )
6 return c;
7
8 for ( int i = 0 ;i < arr.Length;i ++ )
9 {
10 c += arr[i];
11 }
12
13 arr = new int [ 3 ] { 20 , 30 , 40 };
14
15 arr[ 0 ] = 8888 ;
16
17 return c;
18
19 }
20
21 static void Main( string [] args)
22 {
23
24 Console.WriteLine( " 按引用类型按值传递,params参数*************************** " );
25
26 int [] a3 = new int [ 3 ] { 1 , 2 , 3 };
27
28 Console.WriteLine( " 原数组值:{0},;{1};{2} " , a3[ 0 ], a3[ 1 ], a3[ 2 ]);
29
30 Console.WriteLine( " 原数组值和:{0} " , count(a3));
31
32 Console.WriteLine( " 原数组值:{0},;{1};{2} " , a3[ 0 ], a3[ 1 ], a3[ 2 ]);
33
34 Console.ReadLine();
35       }

结果:

按引用类型按值传递,params参数***************************

原数组值:1,;2;3

原数组值和:6

原数组值:1,;2;3

 

2)引用类型按引用传递(地址传递)

 

 

代码
   
   
1 static int count( ref int [] arr)
2 {
3 int c = 0 ;
4 if (arr.Length == 0 )
5 return c;
6
7 for ( int i = 0 ; i < arr.Length; i ++ )
8 {
9 c += arr[i];
10 }
11
12 arr = new int [ 3 ] { 20 , 30 , 40 };
13
14 arr[ 0 ] = 8888 ;
15
16 return c;
17
18 }
19
20
21
22 static void Main( string [] args)
23 {
24
25 Console.WriteLine( " 按引用类型按地址传递,参数*************************** " );
26
27 int [] a3 = new int [ 3 ] { 1 , 2 , 3 };
28
29 Console.WriteLine( " 原数组值:{0},;{1};{2} " , a3[ 0 ], a3[ 1 ], a3[ 2 ]);
30
31 Console.WriteLine( " 原数组值和:{0} " , count( ref a3));
32
33 Console.WriteLine( " 原数组值:{0},;{1};{2} " , a3[ 0 ], a3[ 1 ], a3[ 2 ]);
34
35 Console.ReadLine();
36        }

 

结果:

按引用类型按地址传递,参数***************************
原数组值:1,;2;3
原数组值和:6
原数组值:8888,;30;40

 

 

3),params参数的意义,参数的可变性

 

代码
   
   
1 static int count( params int [] arr)
2 {
3 int c = 0 ;
4 if (arr.Length == 0 )
5 return c;
6
7 for ( int i = 0 ;i < arr.Length;i ++ )
8 {
9 c += arr[i];
10 }
11
12 arr = new int [ 3 ] { 20 , 30 , 40 };
13
14 arr[ 0 ] = 8888 ;
15
16 return c;
17
18 }
19
20 static void Main( string [] args)
21 {
22
23 Console.WriteLine( " 按引用类型按值传递,参数*************************** " );
24
25 int [] a3 = new int [ 3 ] { 1 , 2 , 3 };
26
27 Console.WriteLine( " 原数组值:{0},;{1};{2} " , a3[ 0 ], a3[ 1 ], a3[ 2 ]);
28
29 Console.WriteLine( " 原数组值和:{0} " , count(a3));
30       Console.WriteLine("原数组值和:{0}", count(1,2,3));
31 a3 = new int [ 2 ] { 1 , 2 };
32
33 Console.WriteLine( " 原数组值:{0},;{1} " , a3[ 0 ], a3[ 1 ]);
34
35 Console.ReadLine();
36
37 }

结果:

按引用类型按值传递,参数***************************
原数组值:1,;2;3
原数组值和:6

原数组值和:6
原数组值:1,;2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值