1

I would like to declare an alias to the static class : System.Configuration.ConfigurationManager;

I would like this alias to be available to all the methods of my class so I've tried to do that :

public class MyClass 
{
   using conf = System.Configuration.ConfigurationManager;

   public void MethodOne()
   {
      string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
   }
}

But the above raises the following error : Invalid token 'using' in class, struct, or interface member declaration

Is there something I can do to alias this configuration manager class ?

4 Answers 4

1

using directives can be used to create an alias within the scope of the current .cs file.

using conf = System.Configuration.ConfigurationManager;

namespace MyNamespace
{
    public class MyClass 
    {
        public void MethodOne()
        {
            string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
        }
    }
}

See this documentation for more information

Sign up to request clarification or add additional context in comments.

2 Comments

I give you the answer because you were the first to explain that I could use the using inside the file and outside the Namespace Thanks !
Either way works, actually. The docs give examples of using directives both inside and outside the namespace scope.
1

You can't have a using statement inside a class. The error message you are getting is very specific in this regards.
To solve it, simply put it outside of your class:

using conf = System.Configuration.ConfigurationManager;

public class MyClass 
{
    public void MethodOne()
    {
        string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
    }
}

Comments

1

You must place that using statement outside of your class definition just like the compiler is telling you.

using conf = System.Configuration.ConfigurationManager;

public class MyClass 
{
   public void MethodOne()
   {
      string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
   }
}

Please refer to the C# language specification under section 9.4 "Using directives".

Comments

1

using to create aliases must be used outside a class scope, but in in the assembly or namespace scope!

// Here here here here here here :)
using conf = System.Configuration.ConfigurationManager;

public class MyClass 
{
   public void MethodOne()
   {
      string appConfigStr = conf.AppSettings["someAppSettings"].ToString()
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.