C#特殊方法

1、ref参数方法

ref用于修饰方法的参数,表示该参数为引用传递,而不是值传递,例如:
[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Fuction  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int testParameter;  
  14.             testParameter = 10;  
  15.             RefTest(ref testParameter);  
  16.             Console.WriteLine(testParameter);  
  17.             Console.ReadKey();  
  18.         }  
  19.   
  20.         private static void RefTest(ref int parameter)  
  21.         {  
  22.             parameter = parameter + 1;  
  23.         }  
  24.     }  
  25. }  
需要注意的地方有一下几点:
  1. ref参数类型的方法在定义与调用的时候都需要加ref
  2. ref参数在调用前必须初始化,这个与out参数不同,后者不需要初始化,这也是两者的区别 
  3. 希望在调用函数时候修改被传入的参数时候可以使用ref类型的参数

2、out参数方法

out参数与ref参数一样也是引用传递,不是值传递,调用方法也一样,需要在定义方法与调用方法时候都加上out修饰符,但是它们之间的区别out参数在调用前不必初始化,它可以在方法中初始化,在方法调用结束后更新它的值,用于在需要返回多个返回值的应用场合,用out参数作为一个返回值,return返回一个值。例程如下:
[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Fuction  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int testParameter;  
  14.             testParameter = 10;  
  15.             int a = 10, b = 20, c, d;  
  16.             RefTest(ref testParameter);  
  17.             c = OutTest(a, b, out d);  
  18.   
  19.             Console.WriteLine(testParameter);  
  20.             Console.WriteLine($"a+b={c},b-a={d}");  
  21.             Console.ReadKey();  
  22.         }  
  23.   
  24.         private static void RefTest(ref int parameter)  
  25.         {  
  26.             parameter = parameter + 1;  
  27.         }  
  28.   
  29.         private static int OutTest(int a, int b, out int d)  
  30.         {  
  31.             d = b - a;  
  32.             return a + b;  
  33.         }  
  34.     }  
  35. }  

3、params可变参数

params表示可以变的参数,方法中只允许一个该参数,且必须是最后一个参数,使用方法如实例:

[csharp]  view plain  copy
  1. <span style="font-family:SimSun;">using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Fuction  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int testParameter;  
  14.             testParameter = 10;  
  15.             int a = 10, b = 20, c, d;  
  16.             RefTest(ref testParameter);  
  17.             c = OutTest(a, b, out d);  
  18.             int m1 = 5, m2 = 6, m3 = 7, m4 = 8;  
  19.             Console.WriteLine(testParameter);  
  20.             Console.WriteLine($"a+b={c},b-a={d}");  
  21.   
  22.             ParamsTest(a, b, m1, m2, m3, m4);  
  23.             Console.ReadKey();  
  24.         }  
  25.   
  26.         private static void RefTest(ref int parameter)  
  27.         {  
  28.             parameter = parameter + 1;  
  29.         }  
  30.   
  31.         private static int OutTest(int a, int b, out int d)  
  32.         {  
  33.             d = b - a;  
  34.             return a + b;  
  35.         }  
  36.   
  37.         private static void ParamsTest(int a, int b, params int[] inter)  
  38.         {  
  39.             int len = inter.Length;  
  40.             Console.WriteLine($"a的值为{a},b的值为{b},数组的程度为{len}");  
  41.         }  
  42.     }  
  43. }</span><span style="font-family:Verdana, 'Lucida Grande', Arial, Helvetica, sans-serif;font-weight:bold;">  
  44. </span>  

4、可以选参数

可以选参数是指该参数在调用时候可以传入,也可以不用传入,不传入则该参数会自动使用默认值,可以选参数遵循的原则如下:

  1. 方法定义时,如果某个参数为可选参数,必须指定默认值
  2. 可选参数的默认值必须是常数或者常数表达式
  3. 可选参数后面的参数必须也是可选的,即可选参数必须在最后

实例与后面的部分一同给出

5、命名参数

一般多个参数的方法,传入参数时候形参与实参的对应是通过参数顺兴一一对应的,而如果通过命名参数传入参数,就不用关心参数的顺序了,因为通过指定形参进行与实参对应,参数:传入的值

直接看例程:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Fuction  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int testParameter;  
  14.             testParameter = 10;  
  15.             int a = 10, b = 20, c, d;  
  16.             RefTest(ref testParameter);  
  17.             c = OutTest(a, b, out d);  
  18.             int m1 = 5, m2 = 6, m3 = 7, m4 = 8;  
  19.             Console.WriteLine(testParameter);  
  20.             Console.WriteLine($"a+b={c},b-a={d}");  
  21.   
  22.             ParamsTest(a, b, m1, m2, m3, m4);  
  23.             KeXuanCanshu(a, true);//传入参数为true  
  24.             KeXuanCanshu(a);//不传入参数,默认为false  
  25.             MingMingCanshu(age: 18, name: "Jack");//顺序与定义时候相反  
  26.             Console.ReadKey();  
  27.         }  
  28.   
  29.         private static void RefTest(ref int parameter)  
  30.         {  
  31.             parameter = parameter + 1;  
  32.         }  
  33.   
  34.         private static int OutTest(int a, int b, out int d)  
  35.         {  
  36.             d = b - a;  
  37.             return a + b;  
  38.         }  
  39.   
  40.         private static void ParamsTest(int a, int b, params int[] inter)  
  41.         {  
  42.             int len = inter.Length;  
  43.             Console.WriteLine($"a的值为{a},b的值为{b},数组的程度为{len}");  
  44.         }  
  45.   
  46.         private static void KeXuanCanshu(int a, bool b = false)  
  47.         {  
  48.             Console.WriteLine($"a={a},b={b},b默认为false");  
  49.         }  
  50.   
  51.         private static void MingMingCanshu(string name, int age)  
  52.         {  
  53.             Console.WriteLine($"姓名为{name},年龄为{age}");  
  54.         }  
  55.     }  
  56. }  

猜你喜欢

转载自blog.csdn.net/baidu_35080512/article/details/80629106
今日推荐