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

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
主站蜘蛛池模板: 盐城市| 锡林浩特市| 霍邱县| 许昌市| 张北县| 嵊州市| 普定县| 滨州市| 高碑店市| 腾冲县| 永吉县| 英吉沙县| 道孚县| 神池县| 巨野县| 个旧市| 靖江市| 洞口县| 东乡族自治县| 什邡市| 双江| 华安县| 扎囊县| 神木县| 峡江县| 于田县| 大余县| 东辽县| 静海县| 张家口市| 宣化县| 清原| 延川县| 葵青区| 佛冈县| 建瓯市| 崇礼县| 会理县| 自治县| 南部县| 卢龙县|