C#: Use a namespace for a specific block of code?

后端 未结 8 1653
天涯浪人
天涯浪人 2021-01-17 12:21

I just have a point of curiosity. I\'m working with SSRS and calling its SOAP methods. I\'ve got stubs generated / Web references created and that\'s all working fine and I

8条回答
  •  庸人自扰
    2021-01-17 12:33

    While not relevant for working with Namespaces, or C#, VB.NET supports the With keyword which can be used as a shortcut for accessing members of an object:

    SomeReallyLongName.Property1 = 1
    SomeReallyLongName.Property2 = 2
    SomeReallyLongName.Property3 = 3
    SomeReallyLongName.Property4 = 4
    SomeReallyLongName.Property5 = 5
    

    Can be rewritten as:

    With SomeReallyLongName
        .Property1 = 1
        .Property2 = 2
        .Property3 = 3
        .Property4 = 4
        .Property5 = 5
    End With
    

    It's not in C#, as you can get very close to the same behavior using other approached:

    • Using shorter variable names
    • Using imports-aliasing (Such as Martin suggested)

提交回复
热议问题