Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a type synonym in C#

Tags:

c#

I hope I missed this in the docs. Is there a way to declare a type synonym in C#?

like image 473
akonsu Avatar asked Oct 07 '10 19:10

akonsu


1 Answers

You can use the using statement to create an alias for a type.

For example, the following will create an alias for System.Int32 called MyInt

using MyInt = System.Int32; 

Alternatively, you can use inheritance to help in some cases. For example

Create a type People which is a List<Person>

public class People: List<Person> { } 

Not quite an alias, but it does simplify things, especially for more complex types like this

public class SomeStructure : List<Dictionary<string, List<Person>>> { } 

And now you can use the type SomeStructure rather than that fun generic declaration.

For the example you have in your comments, for a Tuple you could do something like the following.

public class MyTuple : Tuple<int, string> {   public MyTuple(int i, string s) :     base(i, s)   {   } } 
like image 125
Chris Taylor Avatar answered Sep 22 '22 13:09

Chris Taylor