Enums

Grouping of multiple constants is called enum. Instead of declaring constants one by one, you can group all of them and (declare them or group them) as ENUM.

Public enum employee

{

firstname=“John”

second name = “Smith”

}

Now we can directly call fields in enum as employee.firstname; By default, enums are integers. If you want to specify a different data type, use : and data type like:

Public enum HR:string

{

}

If you don’t specify values in enum, like John, smith in above example, automatically first value would be 0, and all other values are incremented by 1. As best practice, declare values to enums at run time.

If you want the actual value of the enum, you have to cast it.

Example:

using System;
 namespace enumproject
 {
     public enum constantsgroup
     {
         constant1,
         constant2,
         constant3
     }
     class Program
     {
         static void Main(string[] args)
         {
             var result = constantsgroup.constant1;
             Console.WriteLine(result);
            Console.WriteLine((int)result);
         }
     }
 }

Here, you have to specify int data type in console.writeline. if not, it will display, as constant1 in the output. And if you specify int, it will get the value of that variable. Since we didnt specify value for constant1, and enums start at 0, so output would be 0. To get the value from it, you have to specify the data type while you use console.writeline. This is called casting. which is specifying as console.writeline((int)result);

Note: you have to cast the variables if you want to see the actual value in it while you are in enum.

To get an enum variable name from value, ex, you have value 1, and you want to know which variable it is assigned to, you have to use as:

public enum constantsgroup
     {
         constant1=1,
         constant2=2,
         constant3=3
     }
     class Program
     {
         static void Main(string[] args)
         {
             var result = constantsgroup.constant1;
             Console.WriteLine(result);
            Console.WriteLine((int)result);
         }

Console.WriteLine((constants group)1); // output will be constant1. It will see what is the value of 1, and get its variable name. If you have same value to different variables, like constant1=1 and constant2=1, then output would be constant2.

Converting to string:

Every object in csharp has method called .ToString(). You can convert anything to string just by declaring as variablename.tostring(). console.writeline by default always calls tastring method on any value that is passed in. So, to convert a value to string while displaying in output, you just have to say, console.writeline(variablename); it will automatically convert it to string.

If you want to explicitly convert to string, you can use .ToString() method.

Converting string to enum:

Converting a string to a different type is called parsing.

string words = "83";
int number = int.Parse(words);
Console.WriteLine(number);

Here 83 is declared as string, but using parse, we are able to convert it to integer.

Similarly you can convert string to float as:

string value = "100";
 float aaaa = float.Parse(value);
 Console.WriteLine(aaaa);

100 and 83 are already integers, but declared as strings. When you try to convert them to integers, it directly converted to integers without errors. What if you use direct strings in the string value and convert it to integer?

string bbbb = "mountain";
 int kkkk = int.Parse(bbbb);
 Console.WriteLine(kkkk);

This will throw an error because, word mountain is a string. You cannot convert it to integer. So we can use try.parse to check if it can parse.

You can test it out like this:

string kkk = "mountain";
 int value;
 bool res = int.TryParse(kkk, out value);
 Console.WriteLine(res);

Here, res would be bool always because it checks whether this string can be converted to integer or not, which would be true or false.

If you’re looking to convert a string to an integer, tryparse method is probably the right way to go. It’s much more efficient in handling errors and, for this reason, won’t slow your code down nearly as much as the int. Parse method.

Last updated