c#中的Out, params,ref 细说并沉淀

1. Out,params,ref之前先记录平时用的最多的按值传递参数的情况,当然默认情况下参数传入函数的默认行为也是按值传递的。

   1:  //默认情况下参数会按照值传递
   2:  static int add(int x,int y) {
   3:  int ans = x + y;
   4:              x = 1000; y = 2000;
   5:  return ans;
   6:          }
 
   1:  static void Main(string[] args) {
   2:              Console.WriteLine("默认情况下按值传递");
   3:  int x = 3, y = 8;
   4:              Console.WriteLine("调用前:x:{0},y:{1}",x,y);
   5:              Console.WriteLine("调用后的结果:{0}",add(x,y));
   6:              Console.WriteLine("调用后:x:{0},y:{1}",x,y);
   7:              Console.ReadLine();
   8:          }

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

输出的结果跟我们预期的一样:

c#中的Out, params,ref 细说并沉淀

2.Out修饰符

   1:  static void add(int x,int y,out int ans) {
   2:              ans = x + y;
   3:          }

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

Main里面可以这么写:

   1:   int ans;
   2:   add(20, 20, out ans);
   3:   Console.WriteLine("20+20={0}", ans);
   4:   Console.ReadLine();

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

输出的结果当然是:20+20=40啦,这个在原来如果没有用out的话那会出现“使用了未赋值的局部变量”。

那即使我们给ans赋值如:int ans=10;

它在调用add方法之后也不会改变,因为调用的方法根本不知道里面发生了什么。

这样就变成了20+20=10了,显然是错的。

当然Out的最大的亮点,就是在一个函数调用之后可以输出多个参数值。

将上面的方法改为:

   1:  static void setParams(out int x,out string y,out bool ans) {
   2:              x = 2;
   3:              y = "YeanJay";
   4:              ans = true;
   5:          }

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

我们在Main里面可以这么写:

   1:  int x=3;
   2:  string y = "ken";
   3:  bool ans = false;
   4:              setParams(out x,out y,out ans);
   5:              Console.WriteLine("x:{0},y:{1},ans:{2",x,y,ans);
 

结果:

c#中的Out, params,ref 细说并沉淀

显然在调用setParams之后相应的变量已经改变了。

这里对Out要注意的一点,在方法体内部必须对加有out 的参数赋值,不然会出现编译错误:必须对输出类型的参数赋值。

3.ref修饰符

最重要的一点,也是跟Out不同的一点,就是使用ref修饰符的时候需要事先对需要ref的参数赋值,不然就会出现对没有赋值的参数进行

操作的编译错误,而out是可以在事先不对变量进行赋值。

看方法:

   1:  static void SwapStrings(ref string str1,ref string str2) {
   2:  string temp;
   3:              temp = str1;
   4:              str1 = str2;
   5:              str2 = temp;
   6:          }

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

方法很简单,就是交换两个字符串。

看Main里面吧,可以这么写:

   1:  string str1 = "Yean";
   2:  string str2 = "Jay";
   3:              Console.WriteLine("调用前:str1:{0},str2:{1}",str1,str2);
   4:              SwapStrings(ref str1, ref str2);
   5:              Console.WriteLine("调用后:str1:{0},str2:{1}",str1,str2);

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

结果:

c#中的Out, params,ref 细说并沉淀

4.params修饰符

说白了就是一个数据作为参数。

直接看方法:

   1:   static double CalculateAverage(params double[] values) {
   2:              Console.WriteLine("字符串的长度:{0}",values.Length);
   3:  double sum = 0;
   4:  if (values.Length == 0) {
   5:  return sum;
   6:              }
   7:  else {
   8:  foreach (var value in values) {
   9:                      sum += value
  10:                  }
  11:  return (sum / values.Length);
  12:              }
  13:          }

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

一个求平均值的方法,也很简单。

然后看下Main中的写法:

   1:  double[] data = { 2.2,2.5,1.1,5.3};
   2:  double average = CalculateAverage(data);
   3:              Console.WriteLine("最后的平均值:{0}",average);

.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }

结果就不写了。

这里需要注意的:C#中只支持一个params参数,而且必须是放在参数列表的最后一个参数,为了避免歧义。

上一篇:Windows下安装多个Redis实例


下一篇:Redis主从复制及原理