This article explains two very useful concepts of C#. I'll also explain where to use these keywords and how to use them to increase readability and decrease complexities.
Constant
As the name suggests, it means it works in the same way. You must be aware of using this functionality in other programming languages too. In the same fashion it works in C# too.
So now the major question arises, what is the use of this keyword?
Suppose if you find a few lines of code in some project or in any document like this:
if (val = 420)
{
// code will go here
}
So, now can you exactly tell me, what this code is trying to say or what does the condition if (val = 420) refer too. I guess you will get confused or fuzzed in learning the meaning of that specific set of code.
That is what I am trying to explain, if the value of val is changed anyhow in the future or anything else then what will happen is, none can exactly explain this.
So the better way to get out of this problem is to use a constant.
Like in this way:
if (val = Collect)
{
// code will go here
}
Now in this case you can at least determine the proper meaning of the code on the basis of defined constants.
The CONST keyword is used.
What CONSTANT does
- Increase readability in code
- Makes code reliable
- Makes code reusable for future aspects
Example | C# Constant
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello
{
class Program
{
static void Main(string[] args)
{
const int date1 = 11 ;
const int date2 = 12 ;
int date = 10;
if (date > date1 && date < date2)
{
Console.WriteLine("Confrence will be on Wednesday.");
}
else if (date < date1 && date > date2)
{
Console.WriteLine("Confrence will be on Saturday.");
}
else
Console.WriteLine("Confernce will surely get started on Friday.");
Console.ReadLine();
}
}
}
Output Window
Enumeration
You can call enumeration a sibling or cousin of CONSTANT. Because enumeration works like a constant. But the major difference between them is enumeration makes our work easy.
In enumeration we do use the enum keyword.
Example
Suppose we declar several units, if you are using a constant then your code will be like:
int const Wednesday = 10;
int const Friday = 11;
int const Saturday = 12;
int const Sunday =13;
This is looking odd and taking your much space too.
You can do that more easily and in a précised fashion through enumeration as:
Enum Date
{
Wednesday = 10;
Friday = 11;
Saturday = 12;
Sunday =13;
}
What Enumeration Does
- Reduces complexity in code
- Increase readability
- Reduce space complexity
Example | C# Enumeration
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello
{
class Program
{
Enum date
{
Wednesday = 10;
Friday = 11;
Saturday = 12;
Sunday =13;
}
static void Main(string[] args)
{
/* const int date1 = 11;
const int date2 = 12 ; */
int mydate = 10;
if (mydate > (int)date.Friday && mydate < (int)date.Sunday)
{
Console.WriteLine("Confrence will be on Wednesday.");
}
else if (mydate < (int)date.Friday && mydate > (int)date.Sunday)
{
Console.WriteLine("Confrence will be on Saturday.");
}
else
Console.WriteLine("Confernce will surely get started on Friday.");
Console.ReadLine();
}
}
}
Output Window