Posts

Showing posts from December, 2017

C# 7.2 New Features

Version 7.2 is the second release introducing several new features. This release requires Visual Studio 2017 15.5 or later. The new features are... Readonly references In addition to ref and out keywords, C# 7.2 add the in keyword. It is used to specify that the method is receiving a value type by reference but it is not allowed to modify it. This is enforced by the compiler. This can increase performance as strings or structs can be passed by ref. void Test() { var msg = "Error message"; Log(in msg); } private void Log(in string msg) { Console.WriteLine($"{DateTime.Now}: {msg}"); } Another addition is the ref readonly modifier, it can be used to return a read only reference to an object. This can be used to return a static instance and be sure that it won't be modified. private static object staticObject = new object(); public static ref readonly object StaticObject => ref staticObject; Two other additions are the readonly and ref st