官术网_书友最值得收藏!

References

You can now return values by reference from a method as well as consume them. This is a little like working with pointers in C but safer. For example, you can only return references that were passed to the method, and you can't modify references to point to a different location in memory. This is a very specialist feature, but in certain niche situations, it can dramatically improve performance.

Consider the following method:

private static ref string GetFirstRef(ref string[] texts)
{
if (texts?.Length > 0)
{
return ref texts[0];
}
throw new ArgumentOutOfRangeException();
}

You could call this method like so, and the second console output line would appear differently (one instead of 1):

var strings = new string[] { "1", "2" };
ref var first = ref GetFirstRef(ref strings);
Console.WriteLine($"{strings?[0]}"); // 1
first = "one";
Console.WriteLine($"{strings?[0]}"); // one
主站蜘蛛池模板: 运城市| 漳平市| 武陟县| 彰化市| 英超| 东明县| 永泰县| 陇南市| 昌图县| 康定县| 康马县| 平远县| 建阳市| 周宁县| 武乡县| 宿州市| 兴文县| 延长县| 准格尔旗| 大荔县| 惠来县| 洛浦县| 临桂县| 崇文区| 贡嘎县| 安新县| 将乐县| 西安市| 定州市| 桐乡市| 惠水县| 宜都市| 泾源县| 民勤县| 化德县| 六盘水市| 九龙坡区| 昭平县| 习水县| 湾仔区| 娱乐|