0

I have a project with some interfaces that contains some type alias in it, for example like below:

namespace UsingTest
{
    using MyType = System.Collections.Generic.List<int>;

    public interface IUsingTest
    {
        MyType GetList();
    }
}

How can I use MyType in another project like below? (without need to redefine type alias)

namespace UsingTest
{
    public class UsingClass : IUsingTest
    {
        public MyType GetList()
        {
            throw new NotImplementedException();
        }
    }
}

Note: if I want write the above code in the same project, as you can see in How do I alias a class name in C#, without having to add a line of code to every file that uses the class?, I need just declare MyType as global using in the interface file. But this technique does not work between multiple projects.

12
  • 2
    You could simply share the file containing the global using between projects (by tweaking the .csproj), but it's probably better if you don't do this at all. Either declare a more specific type that is shareable (by wrapping the List<int> as a property in another class, for example, or using inheritance, but that's not as appropriate for generic collections) or don't use an alias if the only goal is to save keystrokes (you don't get any more stringent type checking, and List<int> is still very general). Commented Jun 5, 2023 at 8:40
  • 1
    It might be a wise choice to make this it's own class. This way you could create a Core project which is used by all projects. Inside of this Core project you can keep all global additives to your code (think of extension methods for example). This way you can easily manage them/ not pollute te projects themselves Commented Jun 5, 2023 at 8:46
  • 1
    What is the actual problem you are trying to solve? For a list of int, just use List<int>. It is not much longer than MyType, and it would take much less effort to understand. Lack of global alias can be an issue when using much more complicated generic types, but such types tend to be difficult to understand anyway, so might be good to avoid anyway. Commented Jun 5, 2023 at 9:18
  • 1
    If you are having conflicting aliases it's almost certainly worth to start using specific classes to represent your types that aren't going to break if the underling generics change. Type aliases were never intended as a feature to support decoupling; they're intended as a stopgap feature to solve the otherwise insoluble problem with different namespaces declaring the same type names. As an abstraction feature they're largely worthless, as the only thing that's happening is a local rename -- the implementation can't change and nowhere is it enforced that only the new name is used. Commented Jun 5, 2023 at 10:39
  • 1
    Once you have a complex generic type like Dictionary<string, List<(int thingA, string thing B)>> or suchlike that isn't memorable on its own it starts paying off to introduce actual classes to represent them, rather than aliases. As a bonus, you can give these more specific names for the methods and properties than the generic methods. You can still fairly easily use the generic collections as the backing storage fields for those classes. Commented Jun 5, 2023 at 10:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.