Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aliasing multiple classes in C#

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...

like image 510
RUser4512 Avatar asked Aug 07 '15 08:08

RUser4512


2 Answers

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-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body.

In other words, the namespace-or-type-name of a using-alias-directive is 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());
        }   
    }
}
  1. create classes for deeper dependencies
like image 80
ASh Avatar answered Nov 01 '22 19:11

ASh


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...
}
like image 35
Matías Fidemraizer Avatar answered Nov 01 '22 21:11

Matías Fidemraizer