C#重点知识详解(一)
点击次数:49 次 发布日期:2008-11-06 08:08:37 作者:源代码网
|
源代码网推荐 源代码网推荐 第一章:参数 源代码网推荐 源代码网推荐 1。1 IN 参数 源代码网推荐 源代码网推荐 c#种的四种参数形式: 源代码网推荐 一般参数 源代码网推荐 in参数 源代码网推荐 out参数 源代码网推荐 参数数列 源代码网推荐 本章将介绍后三种的使用。 源代码网推荐 源代码网推荐 在C语言你可以通传递地址(即实参)或是DELPHI语言中通过VAR指示符传递地址参数来进行数据排序等操作,在C#语言中,是如何做的呢?"in"关键字可以帮助你。这个关键字可以通过参数传递你想返回的值。 源代码网推荐 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 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 1。2 OUT 参数 源代码网推荐 源代码网推荐 源代码网推荐 你是否想一次返回多个值?在C 语言中这项任务基本上是不可能完成的任务。在c#中"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 源代码网推荐 源代码网推荐 1。3 参数数列 源代码网推荐 源代码网推荐 参数数列能够使多个相关的参数被单个数列代表,换就话说,参数数列就是变量的长度。 源代码网推荐 源代码网推荐 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(" args[{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] 源代码网推荐 源代码网推荐 源代码网供稿. |
