0

I have a C# class with methods that take and return types that look something like this:

Dictionary<ClassWithLongNameA, Dictionary<ClassWithLongNameB, ClassWithLongNameB.ProtectedStructType> >

or this:

Dictionary<ClassWithLongNameB, ClassWithLongNameB.ProtectedStructType>

That makes for some cumbersome method definitions that are long and hard to read. If this were C++ I would make my code more readable by utilizing a typedef but that's not available in C#.

I'm aware that I could use a using statement in a manner similar to typedef in C++ from reading this question, but it doesn't work for me for two reasons:

  1. It only applies to one source file and I need this to work with all of the source files that utilize my class and/or inherit from it

  2. I can't use it if one of the template arguments is a protected or private nested type (as it won't be accessible to the using statement) and that is the case for me

I didn't see any other answers in that question that seemed like a good solution to my issue specifically.

What are some other good ways of dealing with this?

edit: This question is not a duplicate of this question as every answer in that thread suggests a using statement, and I have already explained why that is not an appropriate answer to my question.

Bri Bri
  • 2,169
  • 3
  • 19
  • 44

2 Answers2

3

As suggested in my comment, you could create an alias class that derives from the very complex type name, which would get around your issue of using aliases only applying to a single source file.

public class LongTypeAlias : Dictionary<ClassWithLongNameA, Dictionary<ClassWithLongNameB, ClassWithLongNameB.ProtectedStructType>>
{
}

I'll try and cover some of the benefits/drawbacks, but these lists are by no means comprehensive.

Benefits:

  1. Solving the immediate problem: the type name is now shorter! (because it is a different type)
  2. Applying to every source file in the project (unlike using aliases)
  3. Allowing you to specify types in declarations instead of using var (if that is your preferred coding style)

Drawbacks:

  1. If you rely on reflection, it may not work as expected
    • The alias class has a different System.Type than the type it implements, if you ever do direct comparisons with System.Type objects
    • Potential fix: avoid direct comparisons with System.Type objects, and avoid using them as keys in a dictionary
  2. Code readability may suffer
    • Depending on the type you're overriding, you may lose some important/helpful information indicating what it is the class actually does
    • Potential fix: Comprehensively/concisely name alias types
  3. Project may become unmaintainable due to two types representing the same object
    • Depending on the coding standards of your project, you could experience namespace pollution, too many source files, or general confusion as to "why is this here"
    • Potential fix: Put alias types in a separate namespace, and/or define them in the same file as the aliased class

Of course, none of this covers the root question of why the types are so long in the first place. I would encourage you to attempt refactoring where you are able - ReSharper (and even VS2017 I think) provide a lot of excellent tools that make renaming, extracting classes, etc. very easy.

E. Moffat
  • 3,165
  • 1
  • 21
  • 34
2

Subclass the Dictionary with a shorter name. You don't even need a body.

class ShortName : Dictionary<ClassWithLongNameB, ClassWithLongNameB.ProtectedStructType> {}

Then you can declare your methods with that class.

public ShortName GetDictionary()
{
    var dict = new ShortName();
    dict.Add
    (
        new ClassWithLongNameB(), 
        new ClassWithLongNameB.ProtectedStructType() 
    };
    return dict;
}
John Wu
  • 50,556
  • 8
  • 44
  • 80