例如,如果调用方传递本地变量表达式或数组元素访问表达式,所调用方法会将对象替换为 ref 参数引用的对象,然后调用方的本地变量或数组元素将开始引用新对象。
|
|
|---|
|
当通过引用传递时,不会对值类型装箱。 |
若要使用 ref 参数,方法定义和调用方法均必须显式使用 ref 关键字,如下面的示例所示。
C#
class RefExample
{
static void Method(ref int i)
{
// Rest the mouse pointer over i to verify that it is an int.
// The following statement would cause a compiler error if i
// were boxed as an object.
i = i + 44;
}
static void Main()
{
int val = 1;
Method(ref val);
Console.WriteLine(val);
// Output: 45
}
}
out。
例如,以下代码将不会编译。
C#
class CS0663_Example
{
// Compiler error CS0663: "Cannot define overloaded
// methods that differ only on ref and out".
public void SampleMethod(out int i) { }
public void SampleMethod(ref int i) { }
}
但是,当一个方法具有 ref 或 out 参数,另一个方法具有值参数时,则可以完成重载,如下面的示例所示。
C#
class RefOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
在其他要求签名匹配的情况下(如隐藏或重写),ref 和 out 是签名的一部分,相互之间不匹配。
它们是方法,不能传递到 ref 参数。
使用 ref 和 out 传递数组(C# 编程指南)。
你不能将 ref 和 out 关键字用于以下几种方法:
-
async 修饰符定义。
-
yield return 或 yield break 语句。
示例
传递引用类型参数(C# 编程指南)。
C#
class RefExample2
{
static void ChangeByReference(ref Product itemRef)
{
// The following line changes the address that is stored in
// parameter itemRef. Because itemRef is a ref parameter, the
// address that is stored in variable item in Main also is changed.
itemRef = new Product("Stapler", 99999);
// You can change the value of one of the properties of
// itemRef. The change happens to item in Main as well.
itemRef.ItemID = 12345;
}
static void Main()
{
// Declare an instance of Product and display its initial values.
Product item = new Product("Fasteners", 54321);
System.Console.WriteLine("Original values in Main. Name: {0}, ID: {1}\n",
item.ItemName, item.ItemID);
// Send item to ChangeByReference as a ref argument.
ChangeByReference(ref item);
System.Console.WriteLine("Back in Main. Name: {0}, ID: {1}\n",
item.ItemName, item.ItemID);
}
}
class Product
{
public Product(string name, int newID)
{
ItemName = name;
ItemID = newID;
}
public string ItemName { get; set; }
public int ItemID { get; set; }
}
// Output:
//Original values in Main. Name: Fasteners, ID: 54321
//Back in Main. Name: Stapler, ID: 12345
out(C# 参考)
其他主题了解关于泛型类型参数声明的信息。
例如:
C#
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
尽管作为 out 参数传递的变量无需在传递之前初始化,调用方法仍要求在方法返回之前赋值。
例如,以下代码将不会编译:
C#
class CS0663_Example
{
// Compiler error CS0663: "Cannot define overloaded
// methods that differ only on ref and out".
public void SampleMethod(out int i) { }
public void SampleMethod(ref int i) { }
}
但是,如果一个方法采用 ref 或 out 参数,而另一个方法采用其他参数,则可以完成重载,如:
C#
class OutOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(out int i) { i = 5; }
}
属性不是变量,因此不能作为 out 参数传递。
使用 ref 和 out 传递数组(C# 编程指南)。
你不能将 ref 和 out 关键字用于以下几种方法:
-
async 修饰符定义。
-
yield return 或 yield break 语句。
这使得方法可以有选择地返回值。
C#
class OutReturnExample
{
static void Method(out int i, out string s1, out string s2)
{
i = 44;
s1 = "I've been returned";
s2 = null;
}
static void Main()
{
int value;
string str1, str2;
Method(out value, out str1, out str2);
// value is now 44
// str1 is now "I've been returned"
// str2 is (still) null;
}
参考链接:https://blog.csdn.net/hejisan/article/details/51835630