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

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
主站蜘蛛池模板: 桃园市| 自治县| 城固县| 汕头市| 嘉义县| 永和县| 宿州市| 巴南区| 宝兴县| 牟定县| 武威市| 芷江| 清涧县| 察隅县| 平昌县| 西充县| 南投县| 仲巴县| 华亭县| 蓬溪县| 洛宁县| 双城市| 基隆市| 金秀| 陇西县| 上蔡县| 镇江市| 建德市| 措勤县| 宜兰县| 郸城县| 安多县| 九江县| 和静县| 开封县| 元阳县| 肇源县| 全椒县| 华阴市| 磴口县| 万年县|