Bald Bearded Builder
csharp/alias-any-type-in-csharp-12-2

Alias Any Type in C# 12

C# pattern matching with `is null` and `is not null` avoids misleading results from overloaded equality operators.

Loads from YouTube when you press play

Transcript 2 topics

Alias long type names

0:00

You know, C Sharp has had aliasing for namespaces and name types for a long time. And it's fantastic for avoiding naming conflicts or even simplifying the names of generic types. And if you're not familiar with that, I'll show you an example here. But with C 12, we're getting the ability to add aliases to almost every type. Let's talk about some of them.

First up is tuples. Now, tuples are commonly used as method return types. But if you're having to use the same tuple structure multiple times in a file that can get kind of wordy. But being able to create an alias at the top of your file and reuse that alias throughout your class really simplifies your code. And it really makes it easier for new developers to onboard and kind of understand what you're returning.

Another example is pointers. Now, C sharp developers don't work with pointers very much, but when you do, sometimes it's a little easier to define that pointer type as an alias to make it a little prettier in your code instead of having all the little asterisks and all those things cluttering up your code below.

Alias pointers and generics

0:51

Another example are array types. It's very easy to have really long type names like system collections, generic list of your namespace dot something dot something dot customer. That's a mouthful to write through your whole document. So being able to alias that at the top of your file as a customer list, really cleans up the code in that class. Now, one thing to note, while pretty much every type is aliasable, nullable types are kind of an exception.

If you wanna alias a nullable value type, no problem, go ahead, live your best life. However, nullable reference types are off limits. So yeah, aliasing is a great standard of living improvement for C sharp developers. But I'm curious, other than the ones we've mentioned, how are you thinking about using aliases in your applications? Let me know in the comments below and I'll catch you next time.

Sign in to join in. Reading needs nothing.