C# constant

Is this possible?

I have a class that defines all the constant in my application:
public class MyConstant
{
     public static readonly string abc = "some name";
     //...
     //...
    public static readonly int xyz = 1024;
     //...
     //...
}

It is quite inconvenient to browse through the list, thousands of them.
I would like to re-goup them within this class such that I can acess them something like this:
string a = MyConstant.Group1.abc;
int x = MyConstant.Group2.xyz;

Is it possible?
sepknowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveH_UKCommented:
The easiest way to do this is:

public class GroupSettings1
{
  public static readonly string abc = "abc";
  public static readonly int xyz = 1024;
}

public class MyClass {
  public static readonly GroupSettings1 Group1 = new GroupSettings1();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
icrCommented:
A static class would be better than a non static class I think, as you don't have to instantiate it. In order to get the nested settings you can utilise namespaces:

namespace MyApp.MyConstant
{
    public static class Group1
    {
        public static readonly int xyz = 1024;
        public static readonly string abc = "abc";
    }
}

This has the advantage that you can break the groups out into different files if you wish (though you can just place them one after the other in the same file).

If you have no other reason to make it a static readonly variable, why not make it an actual constant?

public const in xyz = 1024;

This means the compiler can optimise it a lot more aggressively for performance benefits (e.g. it can inline it). Not everything can be made const - if it needs instantiation, but for that you can then just use static readonly with a static constructor.
0
SteveH_UKCommented:
Thanks for the points :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.