I want to declare sort of a configuration class. I want to access the properties as Config.Params1.Prop1
.
I get cannot declare a variable of static type
when I use the code below.
I understand why I get the error, but how do I declare the classes to get what I want?
public static class Config
{
public static Section1 Params1;
}
public static class Section1
{
public static string Prop1 => "...";
public static string Prop2 => "...";
}
CodePudding user response:
You cannot do, it because Params1 is an object, not a class. But You can do this.
public static class Config
{
public static Section1 Params1;
}
public class Section1
{
public string Prop1 => "...";
public string Prop2 => "...";
}
static void Main(string[] args)
{
Config.Params1 = new Section1();
var temp = Config.Params1.Prop1;
Or this, with your existing solution.
var temp = Section1.Prop1;
Or if you want to access like Config.Section1.Prop1
then you can simply do this.
namespace Config
{
public static class Section1
{
public static string Prop1 => "...";
public static string Prop2 => "...";
}
You can also make the class Params1
inside the Config
class.
public static class Config
{
public static class Params1
{
public static string Prop1 => "...";
public static string Prop2 => "...";
}
}
CodePudding user response:
Make your class nested:
public static class Config
{
public static class Params1
{
public static string Prop1 => "...";
public static string Prop2 => "...";
}
}
you need to rename Section1 to Params1 for full effect.