专注于网站技术与网络营销的博客

赞助商链接

« ASP.NET中DataGrid控件应用技巧简述c#生成缩略图 »

ref参数,out 参数,参数数列

C# ref参数,out 参数,参数数列


ref:

 namespace TestRefP
  {
  using System;
  public class myClass
  {
  
  public static void RefTest(ref int iVal1 )
  {
  iVal1 += 2;
  
  }
  public static void Main()
  {
  int i=3; //变量需要初始化
  
  RefTest(ref i );
  Console.WriteLine(i);
  
  }
  }
  }
  
  必须注意的是变量要须先初始化。
  
  结果:
  
  5

out:

 public class mathClass
  {
   public static int TestOut(out int iVal1, out int iVal2)
   {
   iVal1 = 10;
   iVal2 = 20;
   return 0;
   }
  
  public static void Main()
  {
   int i, j; // 变量不需要初始化。
   Console.WriteLine(TestOut(out i, out j));
   Console.WriteLine(i);
   Console.WriteLine(j);
  }
  }
结果:
  
  0 10 20

参数数列:

 using System;
  
  class Test
  {
  static void F(params int[] args) {
  Console.WriteLine("# 参数: {0}", args.Length);
  for (int i = 0; i < args.Length; i++)
   Console.WriteLine("\targs[{0}] = {1}", i, args[i]);
  }
  
  static void Main() {
  F();
  F(1);
  F(1, 2);
  F(1, 2, 3);
  F(new int[] {1, 2, 3, 4});
  }
  }
  
  以下为输出结果:
  
  # 参数: 0
  # 参数: 1
  args[0] = 1
  # 参数: 2
  args[0] = 1
  args[1] = 2
  # 参数: 3
  args[0] = 1
  args[1] = 2
  args[2] = 3
  # 参数: 4
  args[0] = 1
  args[1] = 2
  args[2] = 3
  args[3]

注意使用params和不使用的区别:

使用:就是参数数列。可以传递任意个参数。
不使用:只能传递数组。

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Spirit Build 80710

Copyright 2005 - 2008, 完美生活(专注于网站技术与网络营销的博客). Some Rights Reserved.