The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Constants and Enums in C#  
 
Constants and Enums in C#
 
Please rate:
 
Contstants come in handy when you need to store values that will be used frequently within the system and at the same time will not change often, if ever. You want to avoid hard coding values in you code. It makes maintenance and debugging very difficult.
 
You can create a constants class and then access the value like static variables. The class would look lik this.
 
                            public class Constants
                            {
                              public const string SERVER_NAME = "WOODPECKER";
                              public const int SERVER_TYPE = (int)ServerType.Web;
                              public const float PURCHASE_PRICE = 5995.75F;
                            }
                            
 
And you can access the value stored within it like this.
 
                            Console.WriteLine("The name of the server is: " + 
                                              Constants.SERVER_NAME);
                            
 
Enums on the other hand are used to contain a set of named contants. Basically, it lets you give names to a sequence of integers.
 
                            public enum ServerType { Database, Web, Mail, 
                                                     Proxy, Batch, Application }
 
The above line would set Database = 0, Web = 1, Mail = 2, etc... I can access the their values like this.
 
                            if (Constants.SERVER_TYPE == (int)ServerType.Web)
                            {
                              Console.WriteLine();
                              Console.WriteLine("The type of the server is Web");
                            }
                            
 
I use enums to make my code more readible and if code is easier to read then it will be eaiser to maintain and enhance.
 
Download the source
 
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben