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

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
主站蜘蛛池模板: 仁化县| 庆云县| 镇原县| 东阿县| 灵寿县| 仁布县| 佳木斯市| 彭水| 丽江市| 浦江县| 噶尔县| 虎林市| 临沂市| 黔江区| 平顺县| 黄平县| 康定县| 顺昌县| 石渠县| 万载县| 德庆县| 恩施市| 重庆市| 湖州市| 宁蒗| 孝昌县| 曲麻莱县| 建阳市| 盐城市| 荣成市| 丹江口市| 天峻县| 黔西县| 宁城县| 理塘县| 庆安县| 依安县| 江都市| 清原| 温宿县| 海原县|