- ASP.NET Core 2 High Performance(Second Edition)
- James Singleton
- 140字
- 2021-07-08 09:38:59
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
推薦閱讀
- Learn to Create WordPress Themes by Building 5 Projects
- 編程珠璣(續)
- Visual Basic程序設計(第3版):學習指導與練習
- 秒懂設計模式
- MATLAB定量決策五大類問題
- concrete5 Cookbook
- C++ System Programming Cookbook
- Go語言從入門到精通
- Mastering Android Studio 3
- C#程序設計基礎入門教程
- Android嵌入式系統程序開發(基于Cortex-A8)
- C# 7 and .NET Core 2.0 Blueprints
- Learning iOS Penetration Testing
- Unity與C++網絡游戲開發實戰:基于VR、AI與分布式架構
- Office VBA開發經典:中級進階卷