Cannot declare instance members in a static class in C#
I have a public static class
and I am trying to access appSettings
from my app.config file in C# and I get the error described in the title.
public static class employee
{
NameValueCollection appSetting = ConfigurationManager.AppSettings;
}
How do I get this to work?
Solution 1:
If the class is declared static, all of the members must be static too.
static NameValueCollection appSetting = ConfigurationManager.AppSettings;
Are you sure you want your employee class to be static? You almost certainly don't want that behaviour. You'd probably be better off removing the static constraint from the class and the members.
Solution 2:
It says what it means:
make your class non-static:
public class employee
{
NameValueCollection appSetting = ConfigurationManager.AppSettings;
}
or the member static:
public static class employee
{
static NameValueCollection appSetting = ConfigurationManager.AppSettings;
}
Solution 3:
It is not legal to declare an instance member in a static class. Static class's cannot be instantiated hence it makes no sense to have an instance members (they'd never be accessible).