In my current project I have come across the requirement to create generic classes with the same name, but different numbers of generic parameters. For example:
MyClass<T1>
MyClass<T1, T2>
MyClass<T1, T2, T3>
Given that I want all of these in the same namespace, I am confused as to how to structure and name my classes and files?
If we follow the idea that we should have classes limited to one per file and that files should be in a folder structure that represents namespace hierarchy and that the name of the file should match the name of the class, how do I deal with this situation?
What I am really asking here is what should I name the file that contains MyClass<T1>
, and what should I name the file that contains MyClass<T1, T2>
? I am not asking what the names of the type parameters should be.
9
MyGenericClass`1.cs
MyGenericClass`2.cs
MyGenericClass`3.cs
And so on, where the number after the backtick is the number of generic type parameters. This convention is used by Microsoft.
Alternatively, you can use something like
MyGenericCollectionClass[TKey, TValue].cs
which preserves not only the number of generic type parameters, but also their specific names. Granted, it doesn’t preserve the angle brackets, but we can’t have everything in life we want, can we?
6
In the case of Tuple
and Action
that Pete has mentioned, Microsoft themselves use a single file – see Tuple.cs and Action.cs.
I think it partly depends on whether or not the functionality for all the classes is basically the same. Personally I dislike lumping classes into a single file, but this might be an exception. In the source code where I work I added an autogenerated (using T4) NamedTuple
class which acts in the same way as Tuple
, but with a string name as the first argument into the constructor.
To answer your question, if you don’t want to use a single file, perhaps use MyClass_1.cs for MyClass<T1>
, MyClass_2.cs for MyClass<T1, T2>
, etc.
Neither option is ideal though, so I’d be inclined to suggest the “Microsoft do it this way, so…” argument.
4
Have you asked yourself, if the classes do really have the same intent? If one class is more generic than the other, then it will be a GenericClass, a MoreGenericClass and a MostGenericClass. Imagine that each type parameter of a class adds a new dimension to the class, so it may help to ask what dimension it is.
Lets take this example:
Container<Thing>
MetricContainer<Thing, Metric>
MetricTransportableContainer<Thing, Metric, Transport>
I’m aware, that it is not the best example, but very expressive to show three dimensions:
- inner Dimension, what it can load
- metric Dimension, with which metric it can be loaded, only by count of things or by square measure or by cubical capacity or by weight
- outer Dimension, where it can be loaded.
So you can model transportation of cars:
Container<Cars>
MetricContainer<Cars, CountMetric>
MetricTransportableContainer<Cars, CountMetric, Ship>
Transportation of fluids:
Container<Fluid>
MetricContainer<Fluid, Volume>
MetricTransportableContainer<Fluid, Volume, Shelf>
Transportation of energy:
Container<Energy>
MetricContainer<Energy, ElectricPower>
MetricTransportableContainer<Energy, ElectricPower, Box>
Transportation of sugar, cereals:
Container<CrumblyMaterial>
MetricContainer<CrumblyMaterial, Weight>
MetricTransportableContainer<CrumblyMaterial, Weight, Silo>
Oh, what a surprise: a List<T>
has one dimension that represents the things the list can hold; and this is a Map<T, S>
with two dimensions that represent the things the map can hold and the access keys.
12