方法参数
●值参数 :一个值参数相当于一个局部变量,当使用值参数的时候,将会分配一个新的存储位置,将实参拷贝到该位置,并将该拷贝值传递给该方法。因此,值参数只能将值带进方法,但是不能带出方法,而不会影响实参的值。
●引用参数:当使用引用参数的时候,将不会分配一个新的存储位置,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();
}
}
}
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);
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();
{
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]);
}
}
}
}
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]);
}
}
}
}
本例效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?