using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//说明可选参数和命名参数namespace ConsoleApplication3
{classProgram{staticvoidMain(string[] args){int a =2;double b =45;int c =23;char d ='Y';Sam(a, b, c);Sam(a, b, d: d);//d:d成为命名参数,第一个d指缺省值,第二个指主函数中的d,将主函数中参数传给缺省值}staticvoidSam(int a,double b,int c =1,char d ='R')//此处给d赋值是一个缺省值,当主函数没给d赋值时,默认值为R。这样即为可选参数{
a = a +1;
b = a + c;}}}
不定长参数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;//使用变长参数表,求若干整数的平均值namespace ConsoleApplication3
{classProgram{staticvoidMain(string[] args){
Console.WriteLine(Average(1));//传递一个参数,求平均值
Console.WriteLine(Average(1,2,3,4));//传递多个参数,求平均值}staticdoubleAverage(paramsint[] a)//传递不定长的参数,使用了关键字params{int b = a.GetLength(0);double s =0;foreach(int k in a)
s += k;return s / b;}}}