C#: How to create “aliases” for classes

前端 未结 7 1622
你的背包
你的背包 2020-12-17 18:43

I don\'t even know if it\'s called an alias, but let me continue anyway.

You know how the System.String type in C# is sorta \"aliased\" with \"string\"? In Visual S

相关标签:
7条回答
  • 2020-12-17 19:17

    string is a convenience offered by the C# language to refer to the actual CLR type System.String.

    You can achieve similar results with a using directive. At the top of the file where you'd like the name shortened, add this line:

    using ShortName = Namespace.ReallyLongClassNameGoesHereOhGodWhy;
    

    Then you can say ShortName myVar = new ShortName();

    Note that you have to do this on a per file basis. You can't get the same coverage as string with a single declaration.

    0 讨论(0)
  • 2020-12-17 19:25

    You create an alias using:

    using alias = LongClassName;
    

    That alias is visible in the namespace where you declared it.

    0 讨论(0)
  • 2020-12-17 19:26

    You can add a using directive at the top of your file:

     using Rppr = MyProjectNs.RelocatedPlantProductReleaseItem;
    

    But it (only) works per file, not for a Project.

    0 讨论(0)
  • 2020-12-17 19:29

    Try

    using Rppr = SomeNamespace.RelocatedPlantProductReleaseItem;
    

    Reference

    0 讨论(0)
  • 2020-12-17 19:33

    try to add the following with your other usings

    using Rppr = YourItemsNamespace.RelocatedPlantProductReleaseItem;
    
    0 讨论(0)
  • 2020-12-17 19:34

    Use a using directive

    using Rppr = Namespace.To.RelocatedPlantProductReleaseItem;
    

    EDIT Op clarified to ask for a global solution.

    There is no typedef style equivalent in C# which can create a global replacement of a type name. You'll need to use a using directive in every file you want to have this abbreviation or go for the long version of the name.

    0 讨论(0)
提交回复
热议问题