12. How to program in C# - ENUMS - Beginner Tutorial

preview_player
Показать описание
In this video we take a look at enums and why they are useful.

Difficulty of lesson: Novice.

····················································································

Want to make powerful games, Windows and Mac software or mobile applications? Then this course is a great place to start.

····················································································

········································­­·······································­·­····

► All content by Brackeys is 100% free. I believe that education should be freely available to everyone. Want to support me in my mission?

········································­­·······································­·­····

♪ Baby Plays Electro Games
Рекомендации по теме
Комментарии
Автор

I find it funny how you swear in the middle of the video, out of no where to express your point. Hope your channel grows!

HappyKillerO
Автор

Enums, making code not easy to "fuck up". I love this guy, subscribed.

TheBellite
Автор

Your voice is so calming, and your teaching ways have so far been proven to work exceptionaly, at least for me, please continue with this series, it's helping my education a whole lot.

dgudovic
Автор

I think everyone was like "wait, did he just swear?"

ethansantiago
Автор

I used to watch your videos 4 years ago in 2013, Just came back, so much nostalgia, it just calms me for some reason

vanshyadav
Автор

Glad I found these tutorials! There are times when the MSDN site can only explain so much. Thanks for sharing your knowledge.

crystalsoulslayer
Автор

"and not 'fuck up' so to say" had me rolling. Just the delivery bro. Great comedic timing.

jessepaulsen
Автор

This is what I managed to take away from the explanation.

Enums are a way write more readable code and a way to code a faster if you're willing to set up an enum.

Great video.

I.D.M.
Автор

You explained it a lot easier and faster than my professor. Thanks a bunch! Your humor's refreshing as well.

jamesinbou
Автор

Don't forget to like this video, people have no idea how important support is!

enemydisabler
Автор

i like hearing the clicky of the keyboard.

Layarion
Автор

"I was really busy"
(Notice heartstone new on screen )

vylemsobotka
Автор

Excellent work you got me to programming again. I was learning python few months ago but I still wanted to learn something like C# or C++ Thank you for doing this tutorials! ;)

tr
Автор

very nice explanation of Enum..I really like how you compared Switch to Enum to show the advantage of Enum over switch..

marhsall-bwkv
Автор

OMG! Finally I undestood the concept with example of ENUMS, ty!

irvingvi
Автор

ENUMERATIONS:
- a type consisting of a bunch of constants that we associate with a name
- the main purpose of enumerations is to replace the numeric values, which we would use,
if there were no enumeration types. In this way the code becomes simpler and easier to read.
public enum CoffeeSize
{
Small=100,
Normal=150,
Double=300
}

instead of:
const int coffeeSizeSmall = 100;
const int coffeeSizeNormal = 150;
const int coffeeSizeDouble = 300;

- an enumeration is like a class, but we can only declare constants in an enum
- the constants of enumerations can be used in switch-case structures
public double CalcPrice(CoffeeSize coffeeSize) {
switch (coffeeSize)
{
case CoffeeSize.Small:
0.20;
case CoffeeSize.Normal:
0.40;
case CoffeeSize.Double:
0.60;
default:
new coffee quantity: " + (int)coffeeSize);
}
}
[ As we can see in this example, the possibility for the users of our method to provoke unexpected behavior of the method is negligible,
because we force them to use specific values as arguments, namely constants of enumerated CoffeeSize type.
This is one of the advantages of constants, which are declared in enumeration types to constants declared in any class. ]

- enumerations can take values only from the constants listed in the type
- an enumerated variable can have as a value one of the listed in the type constants but cannot have value null
- enumerations are a set of constants of type – this listed type (in the example below the type is Day)
enum Day
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
- each constant, which is declared in one enumeration, is being associated with a certain integer[as their index] (if they are not explicity assigned)
(like in array, it starts with 0)
in the example above, Friday is 5 and Monday is 1.
- it is possible to change the numerical value of constants in an enumeration
- e.g. Sunday = 10 (so the value of Sunday will be 10 instead of 0, the rest of the days following will be Monday = 11, Tuesday = 12 and so on...)
[we can only assing integers as values to the constants]
- enums can also be used like this:
public enum CoffeeSize //in CoffeeSizes.cs
{
Small=100,
Normal=150,
Double=300
}

//called in Cofee.cs
public CoffeeSize size;

public Coffee(CoffeeSizes size)
{
this.size = size;
}

public CoffeeSize Size
{
get { return size; }
}

Coffee normalCoffee = new Coffee(CoffeeSize.Normal);
Console.WriteLine($"The {normalCoffee.Size} coffee is {(int)normalCoffee.Size} ml."); //prints: The Normal cofee is 150 ml.

- Whenever possible, use enumerations instead of set of constants declared in a class.

zerosandones
Автор

Thanks brackeys! I adore this series, can't wait until the next episode! :D

JasperLaw
Автор

Solid video and examples. Keep up the great work!

zeroskill
Автор

i love brackeys. he's a great teacher!

kimcorson
Автор

In my opinion it's like an array and some switch I found this program out there I hope you find it useful
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
class Program
{
/* The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. ...
Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience.*/

public enum Days : int
{Monday = 1,
Tuesday,
Wednesday,
Thursday = 90,
Friday,
Saturday,
Sunday};

static void Main(string[] args)
{
var day = Days.Monday;
Console.WriteLine ($"{day}");
Console.WriteLine ($"{(int) day}");
Console.WriteLine ($"{(Days) 3}");

Console.WriteLine ($"{(int) Days.Monday}");
Console.WriteLine ($"{(int) Days.Tuesday}");
Console.WriteLine ($"{(int) Days.Wednesday}");
Console.WriteLine ($"{(int) Days.Thursday}");
Console.WriteLine ($"{(int) Days.Friday}");
Console.WriteLine ($"{(int) Days.Saturday}");
Console.WriteLine ($"{(int) Days.Sunday}");

foreach (var theEnum in Enum.GetValues(typeof(Days)))
{
Console.WriteLine ($"{(theEnum)}");
}
if (day == Days.Monday)
{
Console.WriteLine ("The conditional was activated");
}
}
}
}

sergiorodrigo
welcome to shbcf.ru