参数数组

C#允许为函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中的最后一个参数,称为参数数组。参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它们。

参数数组可以简化代码,因为不必从调用代码中传递数组,而是传递同类型的几个参数,这些参数放在可在函数中使用的一个数组中。

参数的数量不受限制,可以为0个。

引用参数

用作ref参数的变量有两个限制。首先,函数可能会改变引用参数的值,所以必须在函数调用中使用“非常量”变量。

其次,必须使用初始化过的变量。C#不允许假定ref参数在使用它的函数中初始化。

输出参数

除了按引用传递值之外,还可以使用out关键字,指定所给的参数是一个输出参数。out关键字的使用方式与ref关键字相同。实际上,它的执行方式与引用参数完全一样,因为在函数执行完毕后,该参数的值将返回给函数调用中使用的变量。但是,存在一些重要区别。

l  把未赋值的变量用作ref参数是非法的,但可以把未赋值的变量用作out参数。

l 在函数使用out参数时,out参数必须看做是还未赋值。

即调用代码可以把已赋值的变量用作out参数,存储在该变量中的值会再函数执行时丢失。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
//parameter array - params
static int SumVals(params int[] vals)
{
int sum = ;
foreach (int val in vals)
{
sum += val;
}
return sum;
} //reference parameter - ref
static void ShowDouble(ref int val)
{
val *= ;
Console.WriteLine("val doubled = {0}",val);
} //out parameter - out
static int MaxValue(int[] intArray, out int maxIndex)
{
int maxVal = intArray[];
maxIndex = ;
for (int i = ; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
maxIndex = i;
}
}
return maxVal;
} static void Main(string[] args)
{
int sum = SumVals(, , , , );
Console.WriteLine("Summed Values = {0}",sum); int myNum = ;
Console.WriteLine("myNum = {0}",myNum);
ShowDouble(ref myNum);
Console.WriteLine("myNum = {0}", myNum); int[] myArray = { ,,,,,};
int maxIndex;
Console.WriteLine("The maximum value in myArray is {0}", MaxValue(myArray,out maxIndex));
Console.WriteLine("The first occurence of this value is at element {0}",maxIndex+); Console.ReadLine(); }
}
}

Main()函数

这个函数可以返回void或int,又一个可选参数string[] args。Main()函数可以使用如下4种版本:

static void Main()

static void Main(string[] args)

static int Main()

static int Main(string[] args)

上面的第三、四个版本返回一个int值,它们可以用于表示应用程序如何终止,通常用作一种错误提示(但这不是强制的),一般情况下,返回0反映了“正常”的终止(即应用程序执行完毕,并安全地终止)。

Main()的可选参数args是从应用程序的外部接受信息的方法,这些信息在运行期间指定,其形式是命令行参数。

指定Main()参数:

1)打开项目的属性页面(在Solution Explorer窗口中右击项目名称,选择Properties选项)。

2)选择Debug页面,在Command Line Arguments设置中天街所希望的命令行参数。

结构函数

结构可以包含函数和数据。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
struct customername
{
public string firstName, lastName;
public string Name()
{
return firstName + " " + lastName;
}
} static void Main(string[] args)
{
customername myCustomer;
myCustomer.firstName = "Steven";
myCustomer.lastName = "Wang";
Console.WriteLine(myCustomer.Name()); Console.ReadLine(); }
}
}

委托(delegate)是一种可以把引用存储为函数的类型。委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字。委托的声明制定了一个返回类型和一个参数列表。

在定义了委托后,就可以声明该委托类型的变量。接着把这个变量初始化为与委托有相同返回类型和参数列表的函数引用。之后,就可以使用委托变量调用这个函数,就像该变量是一个函数一样。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
delegate double ProcessDelegate(double param1, double param2); static double Multiple(double param1, double param2)
{
return param1 * param2;
} static double Divide(double param1, double param2)
{
return param1 / param2;
} static void ExecuteFunction(ProcessDelegate process)
{
Console.WriteLine(process(, ));
} static void Main(string[] args)
{
ProcessDelegate process;
Console.WriteLine("Enter 2 numbers separated with a comma:");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
double param1 = Convert.ToDouble(input.Substring(,commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos+,input.Length-commaPos-));
Console.WriteLine("Enter M to multiple or D to divide:");
input=Console.ReadLine();
if(input.ToUpper()=="M")
{
process=new ProcessDelegate(Multiple);
// process = Multiple; // or you can write this
}
else
{
process=new ProcessDelegate(Divide);
// process = Divide; // or you can write this
}
Console.WriteLine("Result: {0}",process(param1,param2)); ExecuteFunction(process); Console.ReadLine(); }
}
}

要把一个函数引用赋给委托变量,必须使用new关键字创建一个新委托。在这个关键字的后面,指定委托类型,提供一个引用所需函数的参数。参数是要使用的函数名,且不带括号。

最新文章

  1. Delphi 使用CreateProcess创建进程并弹出进程PID值 (转)
  2. 使用generator生成排序动画
  3. EffectiveJava——用函数对象表示策略
  4. 记一次SortedDictionary的不当使用
  5. Android init.rc解析【转】
  6. js获取UserControl (&lt;uc1&gt;)控件ID
  7. 一次项目中用到的php函数总结
  8. 记录maven 整合SSM框架
  9. AJAX跨域问题解决思路
  10. sublime 安装package control
  11. Cannot forward to error page for request ......
  12. Android联系人列表实现
  13. 四元数与欧拉角(RPY角)的相互转换
  14. MySQL 创建自定义函数
  15. WPF四则运算《《《《《策略模式
  16. mybatis模糊查询防止SQL注入
  17. 微信小程序:本地资源图片无法通过 WXSS 获取,可以使用网络图片或者 base64或者使用image标签
  18. 【性能调优】:记录一次数据库sql语句性能调优过程
  19. MVC 中使用kindEditor 图片上传在IE 上进行上传出现的问题
  20. RESTful风格与RESTful Api

热门文章

  1. [问题2014S01] 解答
  2. 1012 C语言文法
  3. 使用VB6制作RTD函数
  4. Informix 11.5 SQL 语句性能监控方法及实现
  5. (34)odoo反代理中客户IP处理
  6. i++问题
  7. js——单选框radio
  8. iOS开发UI篇—Quartz2D使用(矩阵操作)
  9. linux getch()实现
  10. 上下文菜单项(contextMenu)----长按按钮弹出菜单项