C# 12 extends using directive support to any type.Now you can use the using alias directive to alias any type, not just named types. That means you can create semantic aliases for tuple types, array types, pointer types, or other unsafe types. Below are few examples :
using Measurement = (string, int); using PathOfPoints = int[]; using DatabaseInt = int?;
You can now alias almost any type. You can alias nullable value types, although you cannot alias nullable reference types.
using Measurement = (string units, int distance);
You can use aliases anywhere you would use a type. For example:
public void F(Measurement x) { }
Aliasing lets you abstract the actual types you are using and lets you give friendly names to confusing or long generic names.
Example-
using Measurment = (string unit, int distance); void MeasurementFunc(Measurment x) { Console.WriteLine(string.Format("{0} {1}", x.distance, x.unit)); } Console.WriteLine("Example - Using directives for additional types"); MeasurementFunc(("Yard", 10)); Console.ReadKey();
Output-
Find out more in the What is new in C#12.
No comments:
Post a Comment