C#中ref、out及特殊說明引數的用法
1.out引數的用法。
2.ref引數的基本用法,相當於c裡面的指標。
3.有ref與無ref的比較。
4.此處應用了類,注意類是引用型別,直接進行引用,除非在函式內生成新的類。
5.ref,out,與無參引數的比較。
using System;
class TestOut
{
static public void FillArray(out int[] myArray)
{
// 初始化陣列(必須):
myArray = new int[5]{1, 2, 3, 4, 5};
}
static public void Main()
{
int[] myArray; // 初始化陣列(不是必須的!)
// 傳遞陣列給(使用out方式的)呼叫方:
FillArray(out myArray);
// 顯示陣列元素
Console.WriteLine("陣列元素是:");
for (int i=0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
class TestOut
{
static public void FillArray(out int[] myArray)
{
// 初始化陣列(必須):
myArray = new int[5]{1, 2, 3, 4, 5};
}
static public void Main()
{
int[] myArray; // 初始化陣列(不是必須的!)
// 傳遞陣列給(使用out方式的)呼叫方:
FillArray(out myArray);
// 顯示陣列元素
Console.WriteLine("陣列元素是:");
for (int i=0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
2.ref引數的基本用法,相當於c裡面的指標。
using System;
class TestRef
{
public static void FillArray(ref int[] arr)
{
// 根據需要建立一新的陣列(不是必須的)
if (arr == null)
arr = new int[10];
// 否則填充陣列,就可以了
arr[0] = 123;
arr[4] = 1024;
}
static public void Main ()
{
//初始化陣列:
int[] myArray = {1,2,3,4,5};
// 使用ref傳遞陣列:
FillArray(ref myArray);
//顯示更新後的陣列元素:
Console.WriteLine("陣列元素是:");
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
class TestRef
{
public static void FillArray(ref int[] arr)
{
// 根據需要建立一新的陣列(不是必須的)
if (arr == null)
arr = new int[10];
// 否則填充陣列,就可以了
arr[0] = 123;
arr[4] = 1024;
}
static public void Main ()
{
//初始化陣列:
int[] myArray = {1,2,3,4,5};
// 使用ref傳遞陣列:
FillArray(ref myArray);
//顯示更新後的陣列元素:
Console.WriteLine("陣列元素是:");
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
3.有ref與無ref的比較。
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);
i = 'a';
TestNoRef(i);
Console.WriteLine(i);
}
}
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);
i = 'a';
TestNoRef(i);
Console.WriteLine(i);
}
}
4.此處應用了類,注意類是引用型別,直接進行引用,除非在函式內生成新的類。
using System;
// ----------------------------------------
// MyClass definition
public class MyClass
{
public int Value;
// ----------------------------------------
// Tester methods
public static void TestRef(ref MyClass m)
{
m.Value = 10;
}
public static void TestNoRef(MyClass m)
{
m.Value = 20;
}
public static void TestCreateRef(ref MyClass m)
{
m = new MyClass();
m.Value = 100;
}
public static void TestCreateNoRef(MyClass m)
{
m = new MyClass();
m.Value = 200;
}
public static void Main()
{
MyClass m = new MyClass();
m.Value = 1;
TestRef(ref m);
Console.WriteLine(m.Value);
TestNoRef(m);
Console.WriteLine(m.Value);
TestCreateRef(ref m);
Console.WriteLine(m.Value);
TestCreateNoRef(m);
Console.WriteLine(m.Value);
}
}
// ----------------------------------------
// MyClass definition
public class MyClass
{
public int Value;
// ----------------------------------------
// Tester methods
public static void TestRef(ref MyClass m)
{
m.Value = 10;
}
public static void TestNoRef(MyClass m)
{
m.Value = 20;
}
public static void TestCreateRef(ref MyClass m)
{
m = new MyClass();
m.Value = 100;
}
public static void TestCreateNoRef(MyClass m)
{
m = new MyClass();
m.Value = 200;
}
public static void Main()
{
MyClass m = new MyClass();
m.Value = 1;
TestRef(ref m);
Console.WriteLine(m.Value);
TestNoRef(m);
Console.WriteLine(m.Value);
TestCreateRef(ref m);
Console.WriteLine(m.Value);
TestCreateNoRef(m);
Console.WriteLine(m.Value);
}
}
5.ref,out,與無參引數的比較。
using System;
class TestApp
{
static void outTest(out int x, out int y)
{//離開這個函式前,必須對x和y賦值,否則會報錯。
//y = x;
//上面這行會報錯,因為使用了out後,x和y都清空了,需要重新賦值,即使呼叫函式前賦過值也不行
x = 1;
y = 2;
}
static void refTest(ref int x, ref int y)
{
x = 1;
y = x;
}
static void noRefTest(int x, int y)
{
x = 8;
y = x;
}
public static void Main()
{
//out test
int a, b;
//out使用前,變數可以不賦值
outTest(out a, out b);
Console.WriteLine("a={0};b={1}", a, b);
int c = 11, d = 22;
outTest(out c, out d);
Console.WriteLine("c={0};d={1}", c, d);
//ref test
// int m, n;
//refTest(ref m, ref n);
//上面這行會出錯,ref使用前,變數必須賦值
int o = 11, p = 22;
refTest(ref o, ref p);
Console.WriteLine("o={0};p={1}", o, p);
int li = 99, zong = 89;
noRefTest(li, zong);
Console.WriteLine("{0},{1}", li, zong);
}
}
class TestApp
{
static void outTest(out int x, out int y)
{//離開這個函式前,必須對x和y賦值,否則會報錯。
//y = x;
//上面這行會報錯,因為使用了out後,x和y都清空了,需要重新賦值,即使呼叫函式前賦過值也不行
x = 1;
y = 2;
}
static void refTest(ref int x, ref int y)
{
x = 1;
y = x;
}
static void noRefTest(int x, int y)
{
x = 8;
y = x;
}
public static void Main()
{
//out test
int a, b;
//out使用前,變數可以不賦值
outTest(out a, out b);
Console.WriteLine("a={0};b={1}", a, b);
int c = 11, d = 22;
outTest(out c, out d);
Console.WriteLine("c={0};d={1}", c, d);
//ref test
// int m, n;
//refTest(ref m, ref n);
//上面這行會出錯,ref使用前,變數必須賦值
int o = 11, p = 22;
refTest(ref o, ref p);
Console.WriteLine("o={0};p={1}", o, p);
int li = 99, zong = 89;
noRefTest(li, zong);
Console.WriteLine("{0},{1}", li, zong);
}
}
相關文章
- out,ref,params引數傳遞
- Shell常用的特殊位置引數變數說明變數
- c# 方法引數(傳值,傳引用,ref,out,params,可選引數,命名引數)C#
- C#中ref和out的區別C#
- 全面分析C#方法中的ref和outC#
- c#.net中引數修飾符ref,out ,params得區別C#
- Lua中呼叫ref和out修飾引數的函式/過載函式函式
- C#中的ref和out的意義和使用方法C#
- 資料泵的TRANSFORM引數說明及使用ORM
- Java Out Of Memory解決之JAVA_OPTS引數說明與配置Java
- mysql常用引數使用說明及查詢MySql
- TOP引數說明
- mysqldump引數說明MySql
- mysqldump 引數說明MySql
- MySQL引數說明MySql
- C#中的值引數,引用引數及輸出引數C#
- Kafka 配置引數彙總及相關說明Kafka
- c# ref out 區別 比較 彙總C#
- Elasticsearch 引數配置說明Elasticsearch
- kafka 引數配置說明Kafka
- redis 3.0 引數說明Redis
- golden gate 引數說明Go
- oracle引數說明(zt)Oracle
- 【AMM】關於ASM中AMM引數說明ASM
- 插曲:Kafka的生產者原理及重要引數說明Kafka
- Nginx的gzip配置引數說明Nginx
- 編譯引數-ObjC的說明編譯OBJ
- c#之帶有out輸出引數的方法定義及使用示例C#
- Python 中 key 引數的含義及用法Python
- Python中key引數的含義及用法Python
- ORACLE中帶引數、REF遊標及動態SQL例項OracleSQL
- C#中?和??及?:的用法C#
- Oracle Table建立引數說明Oracle
- Oracle Table 建立引數 說明Oracle
- mysqldump引數詳細說明MySql
- mosquitto命令引數說明UI
- Oracle Sequence Cache 引數說明Oracle
- 【MYSQL】MHA引數列表說明MySql