How to write "smarter" enums in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm nick and this video i'm going to take a look at how i can integrate one of the things i'm missing actually from the java world in c shop actually i think it's the only thing i'm missing and that is how java is doing enums and i do appreciate that many of you might not know what i'm referring to so in the beginning of this video i'm going to show you for context how java does enums and then i'm going to show you how we can do something similar in c shop by using a very nice library if you like the live content and you want to see more make sure you subscribe bring the certification bell together later when i have a new video hello everybody nick from the future here i just want to let you know that i just released my very first course under the nick chapstars.com website all about unit testing in c shop it is part of what i call my from zero to hero series of courses and effectively what they are are courses that take you from the very basics of a given topic in this case unit testing and then into some real world applications real world examples exactly as you would see them in big companies around the world and then into some pretty advanced stuff now the first 100 of you who want to buy this course can use this coupon code as you see on the screen right now and you're going to get a 15 discount so check it in the description check it from the screen if you want it buy it if you can't buy it it's totally fine i'm still gonna make two videos a week the same way i used to before this is just extra content that i wouldn't make in youtube anyway so nothing is going away i'm just gonna do this too well thank you very much and back to the video so let's go over here this is intellij id it looks like ryder it's mainly because the same people make it and they use the same base for all their ids it's actually one of the main reasons why i love ryder is because any id i choose i have the same look and feel and cross-platform capabilities anywhere so anyway let's say i wanna have an enum here so something like this let's say enum and let's say subscription so i wanna i wanna have different types of subscriptions here and in fact let's change the subscriptions so i have like a free tier and then i have a premium tier and then i have a vip tier right we're using enough to represent different enumerations now if i go back to the main and i get the free tier here and i do subscriptions dot free i can do a dot out dot print line three tier right and that will just print the thing in the console as you can see it says free similar experience as in c sharp and if i wanna calculate the discount so do something like public static double calculate discount then i would just accept the subscription and i would return the discount so maybe have like a return um switch subscriptions and do something like this say case free and returns nothing no discounter then say case vip no that's the last one okay case vip 50 and then case premium 0.25 right so something like this and then to print the discount so do something like this i would say calculate these counts free tier and then discount for here is so if i delete that and i run the program again and i zoom in you can see that the discount for free tier or hey mr space um is that and that's how you probably do things in c sharp now enums in java are a very special type because they can actually behave to a degree as classes well what do i mean well you can have a constructor here it's a private one so externally you cannot call it but this is what an enum looks like if you don't omit the parameters constructor nothing changed here but now i can have this and why is that important well i can now say string name and then double discount right because the discount is directly a concern of the subscription that's the only thing that knows about the discount and subscriptions exist for discounts that's the thing we don't need a separate service to handle that that's the thing responsible we just had to compromise with making a different method to calculate it because well you can't have logic in an enum but in java you can and what this would look like is i can go here and say name and here is like free so i can have a friendly name and then i can say discount which is 0.0 so no discount and i can do the same with every other subscription here so premium is 25 and vip actually capitalization vip is 0.5 so 50 and yes as you guessed you can also have a private final string name here and also private final double discount and yep you can set them you can say this you can say this name dot name and this discount equals discount and what's the cool thing about it well you can make a getter to expose it so you can say public string name get name and then return the name and then public double discount get discount and return the discount right i can go back here i don't need this method anymore i don't need to separate that logic from where it really belongs and what concerns it and i can say get discount and in fact for the name i can even say dot get name because i might want to have a user friendly name and now i can run this and this is perfectly valid java code and as you can see everything is in place now obviously you don't want to put things here that are not directly tied to this logic but when you have scenarios like this where this solely exists to offer a functionality like this discounts maybe memberships maybe something like this then it's totally fine to have it here a very good example for a use case for this is something like this where you want to have like a planet right all the planets with their mass the radius um you can expose the values through getters by the way for context java doesn't have properties they haven't invented that yet and that's how you can you can use something like this and it's great it's the only thing i'm missing because i can totally see many use cases where i can actually use this in c sharp and i can't but wouldn't it be awesome if there was just some way to bring those smart enums in c sharp i mean you've seen the title that's the whole point i'm gonna show you how to do that now so there's a great package created by steve smith or dallas called smart enum and i'm gonna go ahead and install this in this package i'm gonna show you exactly how it works so let's use the same example now we cannot use enums for this in c sharp it just doesn't support yet i know john skate like back in the day tried to have a proposal for this he never made it in who knows maybe someday it might but many people have advocated for this in c sharp over the years and what i'm going to do is i'm going to say subscriptions and yes this is a class and let's see how we can get similar behavior in c-sharp using that package i'm going to go ahead and i'm going to change that to sealed because i don't need anything to extend it and i'm gonna extend the smart inum class and i'm gonna put the name of my class in the generic here and this will allow me to implement some missing members namely the constructor so what i need to do to get to that same point with subscriptions is create a private static read-only and then the type itself so subscription and name it whatever i would before so free and that is a new and again first goes the name of the thing so free and second goes the value same way that you would have a value in your enum i'm going to do that two more times one for premium and one for vip two and three oh wait did i say private this should be public and this will be public and public so let's see how this would look like on a consumer perspective so you'd have you'd go here and you'd say free equals subscriptions dot free and you might have another free one right three two equal subscriptions dot free now i can say console dot writeline free equals three two and if i execute this you see that this returns true and it returns true because the check actually happens on the value everything else all the references everything else is irrelevant the value itself is the only thing that matters you have some other helper methods as well the same way you do in enums you can say subscriptions dot from name so if you say free you will get free from name right and this is also preform name equal to well all of this so if i run this as you can see this returns true again and you can also have free from value so you can use the actual number here free from value and get i think it's one and this would give you the exact same thing the only thing that matters is the value so that's the basic idea behind it now of course let's see how we can integrate this similar discount logic in this there's actually a few ways to do that the first a very straightforward one as before would be to add here so double discount and then expose it through a property so say double discount remove the setter and set it in this constructor here so you can say 0.0 so nothing 0.25 and also 0.5 right straightforward and here you can just set it so if i go here i can say console.writeline discount was free discount and when i print that i should get in this case zero because the free subscription doesn't have any discount however this isn't the only way you're going to go about this even though this is fine you can also change this to an abstract class and turn this into an abstract property and then remove it from here and as your logic might grow and again this is a very domain-centric approach so it's definitely not for everyone i'm not saying you should go ahead and use this everywhere but when it makes sense for your domain and your code to do it it can bring value so once you do that you can go ahead and create a private field and then you can have classes that implement subscriptions in here only accessible from here so you can say free tier for example and this free tier extends the subscriptions class and of course this has to be a class here as well and then i'm going to implement the missing members and then the discount you can just provide here right so it's point um sorry point zero and that's all you need to do let's change that into a computed property here we go and then i'm gonna do that for the other ones again this more beefy approach only makes sense if you have a bit more logic in it if you have like minor things then you probably don't need this so premium tier and that is 25 and let's say a vip tier and that is 0.5 so what we need to do now is change the name and the value in here so we're going to copy that here and remove that we don't need it anymore it can be internal to our class same goes here and same goes to the last one the vip one here we go and again this is all private we don't expose any of that logic this is for us to just organize things better and then you can just return new free tier and then new premium tier and vip tier let's see how the front end looks it's exactly the same i can still run this and get the exact same experience and with that we have a more logic rich approach to things that before were just enumerations and we had to use all the classes to get some value out of them and by the way you don't compromise on this realization this is still serializable some people might use this in ef as a property i actually think that steve has an entity framework package yeah ef core he has a package for that you can serialize it to message pack protobuf utf-8 autofix json.net there's helper packages for basically every major thing that you can think of so you're not compromising on anything you're just adding a bit more into your logic in your code it is definitely a more niche feature but i thought it's a very nice one to show to you and don't forget we do support open source so if you like this go ahead and give it a star on github it really helps the creators keep working on them that's all i have for you for this video thank you very much for watching special thanks to my patreons for making videos possible if you want to support me as well you can find the link description down below leave a like if you like this video subscribe for more than like this hitting the bell as well and i'll see you in the next video keep coding you
Info
Channel: Nick Chapsas
Views: 72,987
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, clean code, dotnet, enums in C#, enum, smartenum, enums in java, java enum, .net enum, SmartEnum, ardalis, Steve Smith
Id: CEZ6cF8eoeg
Channel Id: undefined
Length: 12min 55sec (775 seconds)
Published: Thu Dec 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.