c# – Type synonym between files


There is a project from several files. File A declares a type synonym, for example:

using MyMap = Dictionary<int, string>;

There is also a function that uses this type as a parameter or / and a return value. Let's say this:

public MyMap foo(MyMap map) { ... }

Because the function is declared public it is accessible from the outside, i.e. including from another file. However, due to the fact that the type synonym ( using ) can only be used within the same file, the following solutions are obtained:

  1. Use the original type in file B , i.e. Dictionary<int, string> ;
  2. Add a similar using to file B ;
  3. MyMap using and make a full-fledged class MyMap (i.e. a wrapper over a dictionary).

Each approach has its own pros and cons. I would like to understand which one is more correct to stop at and why.

Maybe the problem should be solved differently (your version)?


The type synonym is primarily needed to shorten the full name of the class, if you do not want to include the entire namespace and litter intellisense and to resolve the name conflict. Sometimes a synonym is used for the convenience of the programmer writing a particular class.

The less you use inheritance and composition, the clearer your code will be. Everything should be done in moderation. Since you are passing a fairly simple Dictionary class as a parameter, it makes no sense to create a wrapper. Call this parameter myMap and others will understand what it is.

Inheritance, including composition, is applied only if the behavior of the original class changes. Only create new types if you are programming new unique behavior. And to understand the meaningful name of the parameter is enough.

Therefore, in file A, create your synonym if it is more convenient and understandable for you as a programmer and call this parameter a meaningful name. And in file B, just pass Dictionary. But if you prefer, add a using declaration to file B.

Scroll to Top