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

C#基础语法:方法参数详解

2015-06-18 09:37 661 查看

●值参数 :一个值参数相当于一个局部变量,当使用值参数的时候,将会分配一个新的存储位置,将实参拷贝到该位置,并将该拷贝值传递给该方法。因此,值参数只能将值带进方法,但是不能带出方法,而不会影响实参的值。

●引用参数:当使用引用参数的时候,将不会分配一个新的存储位置,In other words,引用参数能将值带进方法,也能带出方法,因而会影响实参的值。如下例:

using System;
namespace prg1
{
class Paramstest
{
//值参数使用演示
public static void Transposition_1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//引用参数使用演示static void Transposition_2(ref int a,ref int b)
{
int temp = a;
a = b;
b = temp;
}
static void Main(string[] args)
{
int a = 25;
int b = 30;
//调用Transposition_1
Console.WriteLine("调用Transposition_1之前a={0},b={1}",a,b);
Transposition_1(a,b);
Console.WriteLine("调用Transposition_1之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
//调用Transposition_2
Console.WriteLine("调用Transposition_2之前a={0},b={1}", a, b);
Transposition_2(ref a,ref b);
Console.WriteLine("调用Transposition_2之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
Console.ReadKey();
}
}
}


●输出参数:根据表层含义猜测其只能输出不能输入方法的参数,我们开始紧随上例验证一下,加入以下代码:

static void Transposition_2(ref int a,ref int b)
{
int temp = a;
a = b;
b = temp;
}

编译器便会提醒a,b未赋值的报错,同样我们也就可以直观的看到,输出参数不能将值带进方法,只能将值输出方法。从下面的例子中可以看出在方法的内部进行了输出参数的赋值操作,因此无论在哪里使用输出参数都必须提前赋值,这也是使用任何类型参数的共性。

//Use of output parameters
static void Transposition_3(string name,out string FistName,out string LastName)
{
int i=name.Length;//Get the length of the string
while(i>0)
{
char chparm=name[i-1];
if (chparm == '.')
{
break;
}
i--;
}
FistName = name.Substring(0,i-1);
LastName = name.Substring(i);
}
//调用Transposition_3
string DoName,Nmark;
Transposition_3("rohelm.X",out DoName,out Nmark);
Console.WriteLine("Domain Name of Myself: {0}",DoName);
Console.WriteLine("The last name of my Domain Name: {0}",Nmark);


●参数数组:简而言之,就是方法传递的单位是个数组,而且可以是一维数组或者交错数组(形如int[][]),但是不能是多维数组(形如;string[,]),可以为参数数组制定一个或多个实参,其中每一个实参都是一个表达式,此外参数数组和同一类型的值参数完全等效。例如下例:
 

class Prmarry
{
public static void Show(params string[] name)
{
Console.WriteLine("Array contains the number of elements: {0}", name.Length);
Console.Write("elements of NameArray:");
for (int i = 0; i < name.Length; i++)
{
Console.Write("{0,10}",name[i]);
}
}
}
//调用Show
string[] NameArray = { "rohelm.X", "Boolean", "rrats" };
Prmarry.Show(NameArray);
Console.ReadKey();

也不知咋搞的,我的输入法和编译器好像在玩躲猫猫,一会不一会的就不支持汉字输入了,我也真能用英语输入了,无奈。

下面是这一日志的参考源码,可以整体分析一下:

using System;
namespace prg1
{
class Paramstest
{
//值参数使用演示
public static void Transposition_1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//引用参数使用演示static void Transposition_2(ref int a,ref int b)
{
int temp = a;
a = b;
b = temp;
}
//Use of output parameters
static void Transposition_3(string name,out string FistName,out string LastName)
{
int i=name.Length;//Get the length of the string
while(i>0)
{
char chparm=name[i-1];
if (chparm == '.')
{
break;
}
i--;
}
FistName = name.Substring(0, i - 1);
LastName = name.Substring(i);
}
static void Main(string[] args)
{
int a = 25;
int b = 30;
//调用Transposition_1
Console.WriteLine("调用Transposition_1之前a={0},b={1}",a,b);
Transposition_1(a,b);
Console.WriteLine("调用Transposition_1之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
//调用Transposition_2
Console.WriteLine("调用Transposition_2之前a={0},b={1}", a, b);
Transposition_2(ref a,ref b);
Console.WriteLine("调用Transposition_2之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
//调用Transposition_3
string DoName,Nmark;
Transposition_3("rohelm.X",out DoName,out Nmark);
Console.WriteLine("Domain Name of Myself: {0}",DoName);
Console.WriteLine("The last name of my Domain Name: {0}"+"\n",Nmark);
//调用Show
string[] NameArray = { "rohelm.X", "Boolean", "rrats" };
Prmarry.Show(NameArray);
Console.ReadKey();
}
}
class Prmarry
{
public static void Show(params string[] name)
{
Console.WriteLine("Array contains the number of elements: {0}", name.Length);
Console.Write("elements of NameArray:");
for (int i = 0; i < name.Length; i++)
{
Console.Write("{0,10}",name[i]);
}
}
}
}


您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 方法参数