I want to (am trying to) make my code more readable. I have been using the following class aliasing.
using Histogram = EmpiricScore<int>;
using FeatureHistogram = Dictionary<string, EmpiricScore<int>>;
But I think something like (note:  I'm attempting to describe FeatureHistogram in terms of Histogram here, rather than EmpiricScore<int>>):
using Histogram = EmpiricScore<int>;
using FeatureHistogram = Dictionary<string, Histogram>;
Seems more readable (the dependencies can go much deeper, what if I create a Hierarchical feature histogram), and easier to re-factor (if I happen to decide that the name Histogram is unfortunate). But the compiler won't do it. Why ? Any way to circumvent this ?
Creating new classes seems a little bit overkill...
But the compiler won't do it. Why ?
compiler won't do it according to C# specification 9.4.1:
A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.
using-alias-directive:
using   identifier   =   namespace-or-type-name   ;
The order in which
using-alias-directivesare written has no significance, and resolution of thenamespace-or-type-namereferenced by ausing-alias-directiveis not affected by theusing-alias-directiveitself or by otherusing-directivesin the immediately containing compilation unit or namespace body.In other words, the
namespace-or-type-nameof ausing-alias-directiveis resolved as if the immediately containing compilation unit or namespace body had no using-directives.
namespace N1.N2 {}
namespace N3
{
    using R2 = N1;          // OK
    using R3 = N1.N2;       // OK
    using R4 = R2.N2;       // Error, R2 unknown
}
options:
1. as M.kazem Akhgary suggested in a comment, define new namespace
demo
using Histogram = System.Collections.Generic.List<int>;
namespace TEST
{
    using FeatureHistogram = System.Collections.Generic.Dictionary<string, Histogram>;
    public class Program
    {    
        public static void Main()
        {
            var x = new Histogram();
            Console.WriteLine(x.GetType());
            var y = new FeatureHistogram();
            Console.WriteLine(y.GetType());
        }   
    }
}
Creating new classes seems a little bit overkill...
I don't find it an overkill because if you design a class which wraps the Dictionary<string, Histogram> (your class should implement IDictionary<string, Histogram> and have a private Dictionary<string, Histogram> property backing the data) you're enforcing reusability, which is one of the best selling points of object-oriented programming.
For example, your implementation would look as follows:
public class FeatureHistorgram : IDictionary<string, Historam>
{
    private readonly Dictionary<string, Histogram> _data = new Dictionary<string, Histogram>();
    public void Add(string key, Histogram value)
    {
        _data.Add(key, value);
    }
    // ... and the rest of IDictionary<TKey, TValue> interface members...
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With