C#: Globally alias a generic class name?

百般思念 提交于 2019-12-01 09:26:36

No such thing as a global alias. What you can do is use type inference to simplify your declarations:

var myLine = (Line<SomeTClass, SomeCClass>)LineFactory.CreateLine(...);

and if that's not specific enough for the inference system, you can make the CreateLine() method generic to enforce it:

var myLine = LineFactory.CreateLine<Line<SomeTClass, SomeCClass>>(...);

and given the name of the LineFactory type, maybe even simplify it some more:

var myLine = LineFactory.CreateLine<SomeTClass, SomeCClass>(...);

This last option feels "right" to me in some way I can't fully articulate. Just in case you need a little help, the method declaration would look like something this:

public static class LineFactory 
{
    public static Line<T,C> CreateLine<T,C>(...) { ... }
}

No, you cannot create a global alias.

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