- 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
推薦閱讀
- 微服務(wù)設(shè)計(jì)(第2版)
- JavaScript:Functional Programming for JavaScript Developers
- Developing Middleware in Java EE 8
- Python Data Analysis(Second Edition)
- Kinect for Windows SDK Programming Guide
- TMS320LF240x芯片原理、設(shè)計(jì)及應(yīng)用
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計(jì)原理(第2版)
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- ASP.NET 4.0 Web程序設(shè)計(jì)
- Arduino Electronics Blueprints
- 深入大型數(shù)據(jù)集:并行與分布化Python代碼
- 程序員面試金典(第6版)
- Python自動(dòng)化運(yùn)維:技術(shù)與最佳實(shí)踐
- PHP面試一戰(zhàn)到底
- SFML Essentials