One feature of Pascal I found very useful was the ability to name a data type, eg
type person: record name: string; age: int; end; var me: person; you: person; etc
Can you do something similar in C#? I want to be able to do something like
using complexList = List<Tuple<int,string,int>>; complexList peopleList; anotherList otherList;
So that if I have to change the definition of the datatype, I can do it in one place.
Does C# support a way to achieve this?
A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program. Type alias do not create new types. They simply provide a new name to an existing type.
Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.
A type alias is basically a name for any type. Type aliases can be used to represent not only primitives but also object types, union types, tuples and intersections.
In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name.
Yes, it's possible. You can write:
using System; using System.Collections.Generic; namespace ConsoleApplication12 { using MyAlias = List<Tuple<int, string, int>>; }
or, if declared outside the namespace:
using System; using System.Collections.Generic; using MyAlias = System.Collections.Generic.List<System.Tuple<int, string, int>>; namespace ConsoleApplication12 { }
then use it as a type:
MyAlias test = new MyAlias();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With