您的位置:首页 > 编程语言 > C#

C#代码示例_函数

2014-02-04 20:33 549 查看
参数数组

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 = 0;
foreach (int val in vals)
{
sum += val;
}
return sum;
}

//reference parameter - ref
static void ShowDouble(ref int val)
{
val *= 2;
Console.WriteLine("val doubled = {0}",val);
}

//out parameter - out
static int MaxValue(int[] intArray, out int maxIndex)
{
int maxVal = intArray[0];
maxIndex = 0;
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
maxIndex = i;
}
}
return maxVal;
}

static void Main(string[] args)
{
int sum = SumVals(1, 4, 6, 9, 2);
Console.WriteLine("Summed Values = {0}",sum);

int myNum = 5;
Console.WriteLine("myNum = {0}",myNum);
ShowDouble(ref myNum);
Console.WriteLine("myNum = {0}", myNum);

int[] myArray = { 1,42,78,26,0,29};
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+1);

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(20, 4));
}

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(0,commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos+1,input.Length-commaPos-1));
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关键字创建一个新委托。在这个关键字的后面,指定委托类型,提供一个引用所需函数的参数。参数是要使用的函数名,且不带括号。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: