BrandGhost

Beginner's Guide to Enums In CSharp - C# Enum Tutorial

Have you heard of Enums in C#? Well, if you're looking to better understand how Enums in C# work, you're at the right video! Let's walk through the basics of how Enums work in this C# Enum Tutorial! Have you subscribed to my weekly newsletter yet? A 5-minute read every weekend, right to your inbox, so you can start your weekend learning off strong: https://subscribe.devleader.ca Check out more Dev Leader content (including full in-depth articles with source code examples) here: https://linktr....
View Transcript
enums are something that exists in many different programming languages and if you're starting in C you've without a doubt heard about using enums as programmers enums give us this nice balance between something that feels like it's human readable as well as being numeric at the same time however because enums are something that looks like a string and an integer at the same time there's a lot of opportunity for misusing them in this video we're going to look at the basics of enums including how we can Define them how their values are assigned and then look at some differences between Str and integers when it comes to enums and before we jump over to visual studio just a quick reminder to subscribe to My Weekly Newsletter and I'll put the link in one of the comments below the newsletter is totally free and with that you get a weekly email that has software engineering topics as well as net examples I try to include exclusive articles as well as Early Access to some of these YouTube videos I feel that my big differentiator from other creators is that as a software engineering manager I can offer you some different perspective that's not just writing code check it out it's totally free and with that said let's jump right over to visual studio and look at some enums so in Visual Studio I've gone ahead and defined a custom enum that is called day of the week now there is a similar one that already exists built into net but this is the one that I'm creating just for this example and you can see that this enum which is spanning from lines 24 through 33 has all of the days of the week defined in the order that they appear I'm starting with Monday you could choose to do this in any which order you want but that's what I'm doing in this example now enums by default start at a value of zero assigned to the first item that you see in this enum so that would mean that Monday has an Associated value of zero with it Tuesday 1 Wednesday 2 and so on and so forth incrementing by one each time to be more verbose this is exactly what the values translate to as you can see here I've just gone ahead and explicitly assigned them values 0 through 6 when you're declaring enims you have the option to either let them be automatically incremented you can assign them directly or you can do a bit of a hybrid where you just give it an initial starting value and what that might look like is starting off with Monday being assigned to the value 100 and then I can allow it to automatically increment from there where Tuesday becomes 1001 Wednesday 102 and so on and so forth so that might be a little bit weird to do so in this example I'm just going to allow it to be implicitly assigned values so we will start at zero and have it automatically increment throughout the rest the days of the week all right with our enum back to its original form let's go see what we can do with this now all right so at the top of my screen now I have an example where we can go investigate what we can do with this enum value so all that I've done is I've assigned the enum value of Monday to a variable called day of the week next I'm just writing out a header that says example one so we can see that in the console and then from there I have a couple of things for us to check so if we were to just write out day of the week to the console what would we expect to see would it say something like day of the week. Monday which is like the full type value and the enum value along with it would we just expect to see Monday because we're going to get a string representation or would we see Zero because the first enum value that we have in that set of days of the week starts at zero well when we go to run this we'll find out what we expect but this is a thought exercise that as I go to explain the next two lines I want you to think through so the next two lines five and six try to play on this a little bit more can we actually equate the day of the week to being the string Monday when we think about enums as programmers we're sort of getting this string representation rating code and that allows us to read it more easily but we also said that that enum value seems to represent an integer value as well so could we actually check to see if Monday is equal to zero well when we go to run this sample application let's see the results that we get all right and here is the output from our program so like I said I have example one being printed at the top but Monday is the result of of us doing console right line and then the enum value of Monday so that means that we're calling two string on that variable under the hood and when we call two string on an enum value it will give us the name of that enum so that's kind of interesting if we wanted to we could seemingly easily convert the enum value to a string that is the name of the enum however what's even more interesting is the following two lines that print false and that's where we were checking if the enum value is equal to the string Monday and it turns out that it's not so you can't just say Monday as a string and equate it to that enum value similarly 0o is also not equal to Monday so that means if you wanted to do equality checks between zero or the string Monday to the enum value of Monday we can't just do it that way we're not able to use a string or an integer strictly to do a comparison let's go look at another example so I have example two on my screen here and what I've done between lines 9 and 12 is I'm casting an integer value of 0 1 3 or 5 to the day of the week enum type so this is going to be an interesting experiment for us because when we were checking on line six if day of the week was equal to zero for Monday we saw false so we know that the enum value Monday is not equal to zero but are we able to cast integers to being an enum value well let's go see what happens when we run this and just to call it out I am skipping over some numbers here so we'll do 0 1 3 and 5 so that's just my disclaimer because the results we see may not be contiguous all right and here is the result from our second example so we can see that when we cast zero to being the enum type we do get Monday and similarly when we go to day one that's going to be Tuesday three is Thursday and five is Saturday so while we cannot do a direct equality check between an integer and an enum but we can can do is cast the integer value to the enum value so if that works for integers what about for Strings well example three I have here but I have it commented out because it in fact does not work if I go and uncomment this code for example three we'll see that line 20 actually has a compilation error on it and we're not allowed to cast a string of Monday to the enim type day of the week so we can do integers to day of the week but we cannot do a string the same way so that's going to wrap up some of the basics of working with enums now there's more to be discussed with enums because in this video what we looked at was that an enum seemingly represents both a string and an integer at the same time but what we noticed was that it looks more like it's a developer facing string when we go to use it so calling two string in this case gave us a day of the week but we could cast integers to the enum type but we could not cast strings to the enum type so in the follow-up video what we're going to look at is how we can do some parsing and type conversion so stay tuned to watch that next and we'll follow up with some other considerations for enum usage so stay tuned and you can check those out next thanks for watching and we'll see you next time

Frequently Asked Questions

What are enums and why are they useful in C#?

Enums are a special data type that allows us to define a set of named constants. They provide a way to represent a collection of related values in a human-readable format while still being numeric under the hood. This makes our code easier to read and maintain.

Can I directly compare an enum value to a string or an integer in C#?

No, you cannot directly compare an enum value to a string or an integer. For example, if you try to compare the enum value for Monday to the string 'Monday' or the integer 0, it will return false. However, you can cast an integer to an enum type.

How do I define an enum in C# and assign values to it?

You can define an enum by using the 'enum' keyword followed by the name of the enum and its values. By default, the first value starts at 0 and increments by 1 for each subsequent value. You can also assign specific values to the enum members if you prefer.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload