Aliasing multiple classes in C#

我们两清 提交于 2019-12-10 02:24:33

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/31873149/aliasing-multiple-classes-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!