麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > C# > 正文

參數(shù)修飾符ref,out ,params的區(qū)別

2023-05-09 18:58:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

params 關(guān)鍵字可以指定在參數(shù)數(shù)目可變處采用參數(shù)的方法參數(shù)。

在方法聲明中的 params 關(guān)鍵字之后不允許任何其他參數(shù),并且在方法聲明中只允許一個(gè) params 關(guān)鍵字。

示例

// cs_params.cs
using System;
public class MyClass
{

public static void UseParams(params int[] list)
{
 for ( int i = 0 ; i < list.Length ; i++ )
 Console.WriteLine(list[i]);
 Console.WriteLine();
}

public static void UseParams2(params object[] list)
{
for ( int i = 0 ; i < list.Length ; i++ )
Console.WriteLine(list[i]);
Console.WriteLine();
}

public static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");

int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
輸出
1
2
3

1
a
test

10
11
12

===============================================

方法參數(shù)上的 out 方法參數(shù)關(guān)鍵字使方法引用傳遞到方法的同一個(gè)變量。當(dāng)控制傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)所做的任何更改都將反映在該變量中。

當(dāng)希望方法返回多個(gè)值時(shí),聲明 out 方法非常有用。使用 out 參數(shù)的方法仍然可以返回一個(gè)值。一個(gè)方法可以有一個(gè)以上的 out 參數(shù)。

若要使用 out 參數(shù),必須將參數(shù)作為 out 參數(shù)顯式傳遞到方法。out 參數(shù)的值不會(huì)傳遞到 out 參數(shù)。
不必初始化作為 out 參數(shù)傳遞的變量。然而,必須在方法返回之前為 out 參數(shù)賦值。

屬性不是變量,不能作為 out 參數(shù)傳遞。

如果兩個(gè)方法的聲明僅在 out 的使用方面不同,則會(huì)發(fā)生重載。不過,無(wú)法定義僅在 ref 和 out 方面不同的重載。例如,以下重載聲明是有效的:

class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(out int i) {i = 10;}
}

而以下重載聲明是無(wú)效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}

有關(guān)傳遞數(shù)組的信息,請(qǐng)參見使用 ref 和 out 傳遞數(shù)組。

示例
// cs_out.cs
using System;
public class MyClass
{
public static int TestOut(out char i)
{
i = 'b';
return -1;
}

public static void Main()
{
char i; // variable need not be initialized
Console.WriteLine(TestOut(out i));
Console.WriteLine(i);
}
}
輸出
-1
b

========================================

方法參數(shù)上的 ref 方法參數(shù)關(guān)鍵字使方法引用傳遞到方法的同一個(gè)變量。當(dāng)控制傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)所做的任何更改都將反映在該變量中。

若要使用 ref 參數(shù),必須將參數(shù)作為 ref 參數(shù)顯式傳遞到方法。ref 參數(shù)的值被傳遞到 ref 參數(shù)。

傳遞到 ref 參數(shù)的參數(shù)必須最先初始化。將此方法與 out 參數(shù)相比,后者的參數(shù)在傳遞到 out 參數(shù)之前不必顯式初始化。

屬性不是變量,不能作為 ref 參數(shù)傳遞。

如果兩種方法的聲明僅在它們對(duì) ref 的使用方面不同,則將出現(xiàn)重載。但是,無(wú)法定義僅在 ref 和 out 方面不同的重載。例如,以下重載聲明是有效的:

class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
但以下重載聲明是無(wú)效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有關(guān)傳遞數(shù)組的信息,請(qǐng)參見使用 ref 和 out 傳遞數(shù)組。
示例
// cs_ref.cs
using System;
public class MyClass
{
public static void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i = 'b';
}

public static void TestNoRef(char i)
{
// The value of i will be unchanged in the calling method
i = 'c';
}

// This method passes a variable as a ref parameter; the value of the
// variable is changed after control passes back to this method.
// The same variable is passed as a value parameter; the value of the
// variable is unchanged after control is passed back to this method.
public static void Main()
{

char i = 'a'; // variable must be initialized
TestRef(ref i); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}
輸出
b
b

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久九九热re6这里有精品 | 99精品视频一区二区三区 | 成人一区二区在线观看视频 | 久色亚洲 | 天天黄色片 | 成人三级电影在线 | 国产精品久久久麻豆 | 欧洲狠狠鲁 | av懂色 | 中国女警察一级毛片视频 | 香蕉视频网站在线观看 | 狠狠干五月天 | 欧美国产精品久久 | 中文在线观看免费视频 | 一级黄色播放 | 在线91观看 | 亚洲国产精品一区 | 内地av在线 | 久久国语对白 | 亚洲小视频在线 | 成人一区三区 | 成人精品一区二区三区中文字幕 | 看免费黄色大片 | 国av在线| 国产一精品一av一免费爽爽 | 国产91精品一区二区麻豆亚洲 | 男女无遮挡羞羞视频 | 日本看片一区二区三区高清 | 少妇一级淫片免费放播放 | 欧美成人影院 | 日韩视 | 成人福利在线视频 | 亚洲精品v天堂中文字幕 | 国产精品爆操 | 思思久而久而蕉人 | 激情视频免费看 | 在线成人免费观看 | 精品亚洲国产视频 | 久久成人视屏 | 国产精品9191 | chinese军人gay呻吟 |