What are the uses of “using” in C#?

后端 未结 29 2934
有刺的猬
有刺的猬 2020-11-21 07:31

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of

29条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 07:50

    You can make use of the alias namespace by way of the following example:

    using LegacyEntities = CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects;
    

    This is called a using alias directive as as you can see, it can be used to hide long-winded references should you want to make it obvious in your code what you are referring to e.g.

    LegacyEntities.Account
    

    instead of

    CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects.Account
    

    or simply

    Account   // It is not obvious this is a legacy entity
    

提交回复
热议问题