- 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
推薦閱讀
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Learning C# by Developing Games with Unity 2020
- Mastering phpMyAdmin 3.4 for Effective MySQL Management
- 假如C語言是我發(fā)明的:講給孩子聽的大師編程課
- Julia Cookbook
- 你不知道的JavaScript(中卷)
- Spring+Spring MVC+MyBatis整合開發(fā)實戰(zhàn)
- Android開發(fā)案例教程與項目實戰(zhàn)(在線實驗+在線自測)
- JavaCAPS基礎(chǔ)、應(yīng)用與案例
- Angular開發(fā)入門與實戰(zhàn)
- ASP.NET Core 2 Fundamentals
- Unity 5.X從入門到精通
- 虛擬現(xiàn)實建模與編程(SketchUp+OSG開發(fā)技術(shù))
- C++服務(wù)器開發(fā)精髓
- Java服務(wù)端研發(fā)知識圖譜