- 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
推薦閱讀
- Web程序設計及應用
- Learning NServiceBus(Second Edition)
- C語言程序設計基礎與實驗指導
- Programming ArcGIS 10.1 with Python Cookbook
- Getting Started with CreateJS
- SEO實戰密碼
- 智能手機APP UI設計與應用任務教程
- Geospatial Development By Example with Python
- 3ds Max印象 電視欄目包裝動畫與特效制作
- 百萬在線:大型游戲服務端開發
- 零基礎學編程系列(全5冊)
- Java面向對象程序設計教程
- 大話代碼架構:項目實戰版
- Building an E-Commerce Application with MEAN
- 零基礎學西門子PLC編程:入門、提高、應用、實例