Declaring a type synonym in C#

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)
  {
  }
}

Perhaps you're looking for Using Alias Directives:

using MyType = MyNamespace.SomeType;

This lets you, in your code, type:

// Constructs a MyNamespace.SomeType instance...
MyType instance = new MyType();

is there a way to declare a type synonym in c#?

No.

You can create an alias with using but that is limited to the 1 file (namespace).

Tags:

C#