How do I split a block of code?

If I need to explicitly refer to a namespace to avoid conflicts with another namespace, how can I do that when you need to refer to it multiple times in the same block of code?

For example:

List<NamespaceA.SomeEnum> myobject = new List<NamespaceA.SomeEnum>()
{
  NamespaceA.SomeEnum.A,
  NamespaceA.SomeEnum.B,
  NamespaceA.SomeEnum.C,
  NamespaceA.SomeEnum.D,
  NamespaceA.SomeEnum.E,
}

      

Is there a way to use shortcut / implicit NamespaceA.SomeEnum

in parameter links?

+3


source to share


1 answer


You can do

using ASomeEnum = NamespaceA.SomeEnum;

      

and then

List<ASomeEnum> myobject = new List<ASomeEnum>()
{
    ASomeEnum.A,
    ASomeEnum.B,
    ASomeEnum.C,
    ASomeEnum.D,
    ASomeEnum.E,
}

      



The directive using

must be at the top level or namespace, but not within a particular type.


Another option is to move the method containing the code of violation to another file and make a partial class.

This allows different namespaces to be used in a different file.

+5


source







All Articles