Alias for Tuple in C# 12

Introduction

Aliastype is one of the new features from C# 12. In this blog, you will learn how to use this alias feature for the tuple type in C#. 

Alias for Tuple

Before C# 12, we can use the aliases for namespaces and named types like delegated, classes, interfaces, and structs. With C# 12, you can use the alias name for int, string, and many more types. Let’s see how to use the alias for the tuple. 

Since C# 12 is in preview, enable it from the project file by adding the LangVersion property, as shown in the below figure. 

Alias for Tuple type

You can define an alias for the tuple as given below,

using point3D = (int, int, int); 
public void TestAlias() {
  point3D point3D = (1, 2, 3);
  Console.WriteLine($"{point3D.Item1}, {point3D.Item2},{point3D.Item3}");
}

Next level, you can also define an alias for each item instead of Item1, Item2, and Item3 and give below. 

using point3D = (int x, int y, int z); 
public void TestAlias() {
  point3D point3D = (1, 2, 3);
  Console.WriteLine($"{point3D.x},{point3D.y},{point3D.z}");
}

Summary

We have seen how to use one of the cool features of C# 12, an alias of any type, with an example of defining an alias for the tuple type. We will see more on the C# 12 feature in my next blog.