C# Enum [Step By Step Tutorial to use C# Enum] - How to use an Enum in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to coding droplets resharp tutorial in this video we are going to see what is c sharp elam and how we can use enum in our c-sharp project so make sure you have subscribed the channel so that you won't miss our upcoming videos tutorial videos also please press the bell icon so that you will get notified once we upload new videos now first let me answer you what is an enum so the an enum is a special class that represents a group of constants and we can also assign some values to the constants in enum okay so i'll do one thing i will create an enum in our sample project and i will explain all the things in more detail so this is our sample console application so in this application we will be creating enum and i will explain you in more detail so first let me create one enum here private okay enum now okay we can do one big gender we are naming the enum as gender so we have two constants in gender one is male and another one seeming okay [Music] now [Music] here in our console application we can directly print the enum values or we can assign this enum value to a variable i will show you both so first let me show how we can directly print or how we can directly print uh the enum value in a console application so for example if i need to print the mail so what i can do is gender dot mail okay now if i run the application in the result you can see it has printed male here okay so this is how i can directly get the string from the enum now i will show you how we can assign the uh the value to a variable or assign this empty variable so see what okay i'm just naming this variable as customer gender okay is equal to gender dot may now what will happen is if you hover the mouse here you can see now the customer gender is of type gender the same em okay and the value mail will be assigned to it so now if i do the same console.writeline customer gender here you can see the same result here it will print mail okay the value what we have assigned here now if i'm assigning female it will change to female there will be no difference c okay now now here you can see the type of this customer general variable is gender okay now say for example i need to get it as a string okay so for example instead of war i'm using string here now it will show an error you cannot implicitly convert type gender to string okay so if i need to convert it to string it is very simple i can just give it to string here now it will now you can see the rhythm or the type of this variable is a string so it will directly print the string same female here in our application okay fine now the next thing what i need to [Music] explain is this enum for all the constants inside enum will be having a value an integer value okay so for me if we have not mentioned we can there is an option with which we can mention the values manually but if we have not mentioned the values so say in this example we have not mentioned any values here so automatically enum will or the application will assign a zero based value so you here you can see if you hover the mouse here you can see male is equal to zero and if you hover the mouse here you can see female is equal to one so automatically it will last like zero one two three in that way if we have more constants here so it will assign 0 1 2 3 4 5 6 and auto incremental values to each constants okay so now see if i need to get the integer value which is assigned for this female what uh say i'm i need that integer value so here i have changed it to int and we don't need it as string so i have removed string now but still it will show the same kind of error cannot implicitly convert gender to ink so for that what we need to do is very simple we can mention hint or we can pass it like this okay now now if i print the customer gender you can see the value one yes for female the auto assigned value or the numeric value is one okay now if i change this to male you can see the auto assigned value will be zero here it is showing zero fine now what i need to show you is we can assign the values manually okay say i'm assigning value five for male okay and for female i need to assign thanks okay now if i'm printing uh here anyway we have selected mail so it should print five now so you can see here now i'm running the application yes it has printed 5 okay now if i change it to female it will print it should print 10 yes it has printed 10 here so the value for female is 10 now let's see how i'm creating some more one more e name to explain some more things about elem okay so here we have uh okay uh say example this is uh age group okay age group so here the first change group is 18 to 25 then i'm sorry oh yeah we cannot assign okay where we can do one thing group 18 to 25 i'm really sorry actually we cannot give the numeric value on the start at the starting of the name so this is group 18 to 25 now next group 25 to 35 the next group 35 to 45 next group 45 to sorry 255 and another group 55 to 65 yeah so we have sorry one two three four five five different age groups and see i am assigning some values so this one i am assigning is 5 and here i am assigning as okay eight and four others i'm not assigning the values okay for this i'm assigning see 16 okay but for uh the group 35 to 45 i'm not assigning the value and for group 55 to 65 i'm not assigning the value okay now if i hover the mouse here you can see it has automatically assigned 9 to group 35 to 45 so what it will do is it will take the previous value so that is 8 and it will do an auto increment here and it will assign the next available or the next value to this element or to this constant in the same way if you check the value here you can see it as 70 so the previous one was having 16 and automatically it has assigned 17 here now say if i have one more group here group 65 to 75 sorry to 75 now for this you can see it has automatically assigned 18 so we have ascent 16 here next 17 has been taken automatically for this group or for this constant and 18 has been taken automatically for this one okay so this is how the value the value assigning the values and all those things work the application will auto assign the values if we have not assigned it manually okay now let me show you we can use this group or this enums inside uh inside a class as well like if we have a object a model class we can assign a particular element type as enum i will show you how it how it is so here i am declaring a class customer okay so now the first element is customer name okay so that is a string type and the second one is again string that is email okay now the third one i need the gender so i'm using gender here and just naming it as customer gender okay now uh next i need the age group of the customer so what i'm doing is i'm giving in the same way age group customer age group get set okay so now we have created a class in which we have these both the enums which we have created now let's see how we can declare the or declare the object using this class now i'm declaring one customer what customer is equal to new customer okay now the first uh we have to give the name so customer name is john second we have to give email so let's see oh sorry uh the email is john i create gmail.com okay now the second one or the third one is the agenda so john automatically it will show me that i have to selected that i have to select a constant from gender enum okay so i can select gender mail now next i need to select an age group so automatically it will show me i have to select an age group from enum so i have selected 35 to 45 that is the age group of john okay [Music] now just printing the values of this customer in our console application sorry yeah so name is customer dot name okay okay anyway i'm not printing email i'm just printing the agenda and age group so age group is customer dot age group and gender is customer dot oh sorry console.writeline 5. now it should print the customer details in our console application so you can see john and each group is 325 to 45 gender mail it has printed everything okay now the next thing what i need to show is so let me remove all these things i just need to show how we can use it in class that's why i have created this now say we are saving uh the value of this gender in our database so we will let's say we are saving we are having a field in our database which is an integer data type and we are saving this value of gender in our database so now what i need to show is how we can convert a integer value to this enum so say we have uh gender id field in our database and we have retrieved the gender id five okay so here five is male right or let's say we can use 10 here for female okay general id is so it is female now if we need to convert this integer value so this is an integer value now so instead of y i will give it for you to understand it clearly so this is an integer value now if i need to create uh the gender enum object or i've created a variable here gender now what i'm doing is i can give it like this gender gender id so now you can see the type is gender okay now here what i'm doing is i'm giving console.writeline gender so now it will print what uh the gender which or the gender constant name which is with value then see automatically we have got the gender female and it has printed here so this is how we can convert an integer to uh enum time okay now i need to show you one more thing so many people asked me how we can i trade through the gender or how we can get the elements or how we can get the constants and values of gender in a list uh say example for example in your application you need those or you have a drop down list and you need to populate this drop down list with the enum constants and you have to assign uh you have to get the value accordingly so for that anyway uh for that if you get this enum constants in a list that will uh with that list you will be able to populate a drop-down or any other list or anything okay so i will just show you how we can get uh this enum to a list okay so first what i'm doing is i'm creating age group i'm creating a variable named age group list okay then enum dot get mails of type of page uh here we have to give that type so age group is of type okay now i'm just moving to the next line for you to see it clearly dot select is such that new okay i'm creating a new dynamic list so here i'm giving age group id is equal to int enum dot of type of again the same type we have to mention here okay then string value a [Music] now [Music] next is age group text is equal to a now i'm converting this this to a list okay five so now this list will be having several objects so each object will be having an h group id that will be an integer value so that will be the value which we have assigned here and after that in the same object we will be having age group text so that will be the name of the constants we have assigned here so i will show you by printing these values in our console application war rc age group in age group list okay console dot write line okay here i am printing is first time printing the integer value so that is h group id now next i am printing uh the age loop text okay now i will show you how it works so here you can see now we have age group 18 to 25 that is five so in our application yeah here you can see each group 18 to 25 is having a numeric value of 5 and 25 to 35 is having 8 and 35 to 45 we have not assigned it here but automatically it has taken 9 then 45 to 55 16 and next two it has automatically taken so this is how we can get uh the new um enum constants in a list so now you can just use this list to populate a list or drop down or anything that grip view or anything okay so hope you got the logic so let me know uh which which kind of emails you you have in your application or which kind of ems you use your in your application which kind means uh whether it is in the same way customer agenda or age group or anything else and let me know your doubts as well also your feedbacks you can comment to the video your doubts feedbacks and everything to the video we will be replying it as soon as possible so hope you enjoyed the video you have got the hope you got the logic so that's it for this video please like the video and see you in the next video thank you all
Info
Channel: Coding Droplets
Views: 1,007
Rating: undefined out of 5
Keywords: c#, c# enum, c# enum class, c# enum description, c# enum example, c# enum flags, c# enum foreach, c# enum parse, c# enum string, c# enum string value, c# enum to int, c# enum to string, c# enum type, c# enum value, c# foreach enum, c# get enum description, c# int to enum, c# parse enum, c# string to enum, c# switch enum, create enum c#, csharp enum, enum step by step tutorial to use c#, enum to int c#, enums c#, foreach enum c#, int to enum c#, string to enum c#
Id: yAtWBN9qTh4
Channel Id: undefined
Length: 21min 51sec (1311 seconds)
Published: Thu Apr 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.