C# Constants – Style – And a Good Pattern

This post come from a question on StackOverflow and question :

In C#, What’s the best way to store a group of constants that my program uses


There would seem to be a very good code pattern for the expression of constants in C# (or any OO programming language).

The Pattern

  • A static class,
  • with read-only properties,
  • these proprieties are also static

The Benefits

  • A static class can be accessed from anywhere (in the same namespace)
  • The namespace the class is in can be used to scope the visibility of the constants.
  • The properties achieve constant behaviour thru not having setter methods.
  • Aesthetically pleasing in an Object Oriented sense. The result is an OO expression of “what a constant is”.

An Example 

    public static class Constants
        public static string SomeConstant
                return "String Constant";
        public float someFloatConstant
                return 3.5F;

Digg This

, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: