- 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
推薦閱讀
- Building Modern Web Applications Using Angular
- Visual FoxPro程序設計教程
- 樂學Web編程:網站制作不神秘
- 算法基礎:打開程序設計之門
- jQuery EasyUI網站開發實戰
- Learning Linux Binary Analysis
- 單片機應用技術
- Learning Laravel 4 Application Development
- STM32F0實戰:基于HAL庫開發
- Instant Ext.NET Application Development
- Python深度學習原理、算法與案例
- Spring Boot+MVC實戰指南
- Mastering Docker
- 零基礎學SQL(升級版)
- Python編程入門(第3版)