[英]What does the ref keyword mean before the return type in C#
在下面的代码中, GetAge()
方法签名中ref
的意义是什么?
public class Person
{
private int age;
public ref int GetAge()
{
return ref this.age;
}
}
ref
返回是C#7.0中的一个新功能。 它允许返回对内存位置的引用。 在以前的C#版本中这是不可能的。 您甚至可以存储返回的内存位置,如下所示:
var person = new Person();
// Here we can store the reference to the memory area and we can modify it
ref int age = ref person.GetAge();
// like this
age = 50;
我们一直在同一个内存位置工作,而不是age
的副本 。
幕后发生了什么?
如果我们有这个代码:
public class Program
{
public static void Main()
{
var person = new Person();
// Here we can store the reference to the memory area and we can modify it
ref int age = ref person.GetAge();
// like this
age = 50;
}
}
public class Person
{
private int age;
public ref int GetAge()
{
return ref this.age;
}
}
以下是编译器(Roslyn)为该代码幕后操作的内容:
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[module: UnverifiableCode]
public class Program
{
public unsafe static void Main()
{
Person person = new Person();
int* age = person.GetAge();
*age = 50;
}
}
public class Person
{
private int age;
public unsafe int* GetAge()
{
return ref this.age;
}
}
好!!! 我认为,我们都高兴,我们不必应付所有这些*
有心计。
这个功能什么时候有用?
通过避免复制值或多次执行解除引用操作,添加ref本地和ref返回使算法更有效。
当您处理值为数据类型( struct
)的大型数据结构并将副本传入和传出方法可能不是非常有效时,它最有用。 例如,假设我们有一个包含一堆struct
对象的类:
class Container
{
private Tile[] tiles = new Tile[] { new Tile { X = 10 } };
public Tile this[int x]
{
get { return tiles[x]; }
set { tiles[x] = value; }
}
}
public struct Tile
{
public int X { get; set; }
// Many more propeties
}
如果我们想使用Tile
对象,因为它们是struct
,我们将无法执行此操作:
var container = new Container();
container[0].X = 10;
我们不能这样做,因为编译器会发出此错误:
错误CS1612无法修改'Container.this [int]'的返回值,因为它不是变量
编译器抛出该错误以明确表明您认为自己正在做的事情(修改索引项),并不是您正在做的事情。 您实际上正在修改副本,因此它会强制您执行此操作。 因此,为了能够设置X,您需要在副本上执行此操作:
var container = new Container();
var copy = container[0];
copy.X = 10;
// now we need to set the item to the copy
container[0] = copy;
正如您所看到的那样效率不高,特别是如果我们使用大型struct
并且我们需要以迭代方式操作其中的许多struct
。
使用C#7.0,我们可以这样做:
public ref Tile this[int x]
{
get { return ref tiles[x]; }
}
现在我们可以直接操作Tile
,而无需发送副本,制作副本,然后将原始项目设置为副本。 像这样:
var container = new Container();
ref Tile tile = ref container[0];
tile.X = 10;
一个小问题
网上有很多例子,它们的语法如下:
// Notice the ref missing on the right side
ref int age = person.GetAge();
这将导致此错误:
无法使用值初始化by-reference变量
正确的语法是让双方的ref
像这样:
ref int age = ref person.GetAge();
更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.