Constants, Literals and Enumerations in C#
In this tutorial we are going to understand the importance of constants, literals and enums. Const and enums are C# constructs which are used to make code more readable and easy to maintain.
How to use Constant
You can make any variable constant by putting “const” keyword at the start of that variable.
Example
Why use Constant
How to use Enumeration
You can use enumeration by using enum word followed by the name you want to specify for your enum and then you can list the entire constant in enum separated by comma.
Example
Consider you have following constants.
Where to define a Enumeration
One thing you should know that you have to define you enum outside the main function. You can’t use them in a function.
Value of Enumeration
By default the enumerations are int type that means you can only assign int values to its member. But you can define types that are numeric in nature that are byte, sbyte, short, ushort, int, uint, long or ulong.
How to define a type of Enumeration
You can define a type of enumeration by just putting colon (:) after the name and giving the type you want to assign.
Example
Enums are very helpful when dealing with a lot of constant. Create example with ten enum elements.
Find if string enums are possible or create any alternative.
<< Data Types: Type conversion
Constant
Constant are just like C# variable with the exception that you have to assign a value at declaration and you can’t change its value once declared.How to use Constant
You can make any variable constant by putting “const” keyword at the start of that variable.
Example
const float PI = 3.14;Note that it is a common practice that when you declare a constant variable you spell it in upper case. It is not compulsory but it is a good programming practice. When others will see your code it would be easy for them to understand that it is a constant.
Why use Constant
- If you have a variable whose value won’t change in throughout your program then you may want it to be constant because you don’t want its value to be changed accidently.
- It is a good programming practice that you don’t use hard coded value in your program because it might get difficult for other programmers when they see your code or even for you if you see your code after some time that why you have used those hard coded value.
- By using constant it is easy to maintain your code because if in future you want to change that value you can change that at declaration and all your code will remain same where as if you use hard coded value it will become difficult to upgrade your change.
class Program { static void Main(string[] args) { Console.Write("Enter radius = "); int radius = Convert.ToInt16(Console.ReadLine()); float area = 3.14f * radius * radius; //bad practice what is 3.14 Console.WriteLine("The area of circle with {0} radius is {1}.", radius, area); Console.ReadLine(); } }Example: good practice
class Program { static void Main(string[] args) { const float PI = 3.14f; Console.Write("Enter radius = "); int radius = Convert.ToInt16(Console.ReadLine()); float area = PI * radius * radius; //good practice PI is constant to use for cirlce Console.WriteLine("The area of circle with {0} radius is {1}.", radius, area); Console.ReadLine(); } }
Enumeration (Enum)
If you have a program with a lot of constant that all related to each other then it is good if you can group them together at one place. For this purpose you can use enumeration.How to use Enumeration
You can use enumeration by using enum word followed by the name you want to specify for your enum and then you can list the entire constant in enum separated by comma.
Example
Consider you have following constants.
const int GRADE_A = 80; const int GRADE_B = 70; const int GRADE_C = 60; const int GRADE_D = 50;you can create enum for these constants like
enum Grades { GRADE_A = 80, GRADE_B = 70, GRADE_C = 60, GRADE_D = 50 }Note that you can’t use space when you defining an element. If needed you can use underscore ( _ ).
Where to define a Enumeration
One thing you should know that you have to define you enum outside the main function. You can’t use them in a function.
Value of Enumeration
- If no value is assigned to first constant then it will have default value that is 0.
- If no value is assigned to constant other than first, it will have value one more than the previous value.
enum Score { NILL, //will have default value of 0 ONE, // it will have one more than previous one that is 1 TEN = 10, //will have assign value ELEVEN //will have one more than previous one that is 11 }Type of Enumerations
By default the enumerations are int type that means you can only assign int values to its member. But you can define types that are numeric in nature that are byte, sbyte, short, ushort, int, uint, long or ulong.
How to define a type of Enumeration
You can define a type of enumeration by just putting colon (:) after the name and giving the type you want to assign.
Example
enum Grades : byte { GRADE_A = 80, GRADE_B = 70, GRADE_C = 60, GRADE_D = 50 }Working Example of Enumeration
namespace Enums { class Program { enum Temperature { FREEZING_POINT, BOILING_POINT = 100 } static void Main(string[] args) { int freezing_Poing = (int)Temperature.FREEZING_POINT; //has to explicitly type cast into integer int boiling_Point = (int)Temperature.BOILING_POINT; //has to explicitly type cast into integer Console.WriteLine("The freezing point of water is {0} Celsius.", freezing_Poing); Console.WriteLine("The boiling point of water is {0} Celsius.", boiling_Point); Console.ReadLine(); } } }
Practice
Create a program with string constant.Enums are very helpful when dealing with a lot of constant. Create example with ten enum elements.
Find if string enums are possible or create any alternative.
<< Data Types: Type conversion
Tags:
C-sharp

Muhammad Abid
Muhammad Abid is software engineer, a passionate programmer who have created many projects in C# and Java.
Get latest updates in your inbox
Popular Posts
How Yoga Nurtures Your Programming Skills
Understanding and analyzing basics of SEO
Creating SEO friendly URLs with PHP and SQL
Adobe AIR - Developing cross platform applications
Writing functions to insert, update, search, delete from SQL tables in PHP
Getting started with C#
Creating XML sitemap with PHP and SQL and submitting to search engines
Creating RSS feed in xml format with PHP and SQL
Creating thumbnails dynamically from images with PHP
Backup SQL database with PHP
Tags
CSV
Validation
Android
Java
Nashorn
PHP Mailer
cPanel
Facebook
Marketing
AdSense
Google
Technorati
Feathers
Starling
crop
PDF
Yoga
feedburner
rss feed
meta tags
SQL
backup database
Adobe Air
XML Sitemap
Syntaxhighlighter
Uploadify
Tinymce
screenshot
Text image
webmaster tools
Verifying website
web analysis
SEO
visual studio 2013
IDE