C#基礎(chǔ)語法:方法參數(shù)詳解
●值參數(shù) :一個值參數(shù)相當(dāng)于一個局部變量,當(dāng)使用值參數(shù)的時候,將會分配一個新的存儲位置,將實(shí)參拷貝到該位置,并將該拷貝值傳遞給該方法。因此,值參數(shù)只能將值帶進(jìn)方法,但是不能帶出方法,而不會影響實(shí)參的值。
●引用參數(shù):當(dāng)使用引用參數(shù)的時候,將不會分配一個新的存儲位置,In other words,引用參數(shù)能將值帶進(jìn)方法,也能帶出方法,因而會影響實(shí)參的值。如下例:
using System;
namespace prg1
{
class Paramstest
{
//值參數(shù)使用演示
public static void Transposition_1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//引用參數(shù)使用演示
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;
//調(diào)用Transposition_1
Console.WriteLine("調(diào)用Transposition_1之前a={0},b={1}",a,b);
Transposition_1(a,b);
Console.WriteLine("調(diào)用Transposition_1之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
//調(diào)用Transposition_2
Console.WriteLine("調(diào)用Transposition_2之前a={0},b={1}", a, b);
Transposition_2(ref a,ref b);
Console.WriteLine("調(diào)用Transposition_2之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
Console.ReadKey();
}
}
}
●輸出參數(shù):根據(jù)表層含義猜測其只能輸出不能輸入方法的參數(shù),我們開始緊隨上例驗證一下,加入以下代碼:
static void Transposition_2(ref int a,ref int b)
{
int temp = a;
a = b;
b = temp;
}
編譯器便會提醒a(bǔ),b未賦值的報錯,同樣我們也就可以直觀的看到,輸出參數(shù)不能將值帶進(jìn)方法,只能將值輸出方法。從下面的例子中可以看出在方法的內(nèi)部進(jìn)行了輸出參數(shù)的賦值操作,因此無論在哪里使用輸出參數(shù)都必須提前賦值,這也是使用任何類型參數(shù)的共性。
//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);
}
//調(diào)用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);
●參數(shù)數(shù)組:簡而言之,就是方法傳遞的單位是個數(shù)組,而且可以是一維數(shù)組或者交錯數(shù)組(形如int[][]),但是不能是多維數(shù)組(形如;string[,]),可以為參數(shù)數(shù)組制定一個或多個實(shí)參,其中每一個實(shí)參都是一個表達(dá)式,此外參數(shù)數(shù)組和同一類型的值參數(shù)完全等效。例如下例:
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]);
}
}
}
//調(diào)用Show
string[] NameArray = { "rohelm.X", "Boolean", "rrats" };
Prmarry.Show(NameArray);
Console.ReadKey();
也不知咋搞的,我的輸入法和編譯器好像在玩躲貓貓,一會不一會的就不支持漢字輸入了,我也真能用英語輸入了,無奈。
下面是這一日志的參考源碼,可以整體分析一下:
using System;
namespace prg1
{
class Paramstest
{
//值參數(shù)使用演示
public static void Transposition_1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//引用參數(shù)使用演示
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;
//調(diào)用Transposition_1
Console.WriteLine("調(diào)用Transposition_1之前a={0},b={1}",a,b);
Transposition_1(a,b);
Console.WriteLine("調(diào)用Transposition_1之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
//調(diào)用Transposition_2
Console.WriteLine("調(diào)用Transposition_2之前a={0},b={1}", a, b);
Transposition_2(ref a,ref b);
Console.WriteLine("調(diào)用Transposition_2之后a={0},b={1}", a, b);
Console.WriteLine("====================\n");
//調(diào)用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);
//調(diào)用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]);
}
}
}
}
相關(guān)文章
C#使用FluentScheduler實(shí)現(xiàn)觸發(fā)定時任務(wù)
FluentScheduler是.Net平臺下的一個自動任務(wù)調(diào)度組件,這篇文章主要為大家詳細(xì)介紹了C#如何使用FluentScheduler實(shí)現(xiàn)觸發(fā)定時任務(wù),感興趣的小伙伴可以了解下2023-12-12
c# 如何對網(wǎng)絡(luò)信息進(jìn)行相關(guān)設(shè)置(ip,dns,網(wǎng)關(guān)等)
這篇文章主要介紹了c# 網(wǎng)絡(luò)適配器的相關(guān)操作,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03

