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

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
主站蜘蛛池模板: 云霄县| 邵武市| 米易县| 开原市| 南城县| 营口市| 舟山市| 罗山县| 东乡县| 博客| 鹤庆县| 贵溪市| 无锡市| 永清县| 盐津县| 虎林市| 怀柔区| 攀枝花市| 洞口县| 大港区| 康保县| 兴宁市| 塘沽区| 蕉岭县| 景宁| 汶川县| 元氏县| 南江县| 乡宁县| 鹤峰县| 崇文区| 鄱阳县| 沙坪坝区| 牙克石市| 古丈县| 吉林省| 利津县| 东兰县| 中卫市| 拉萨市| 绥江县|