This issue describes how to add a new reference document: <enums.md>.
Description
Using enums is a great way to limit the possible values of a variable. It allows you to create your own variable type and define its set of allowable values. You'd have to define the enum first with its possible values then, you can use the defined enum to give your variables that type. That variable can now only have possible values as defined by that enum
For example, you're working with months, you would want to create an enum called Month, and set its allowable values to the 12 possible months. Then you can create a variable of type Month and name it firstMonth. The compiler, or your tool will only allow you to set firstMonth to one of the possible values.
Here's an example using C#:
public enum Month
{
January,
February,
...,
December
}
public class EnumConversionExample
{
public static void Main()
{
Month firstMonth = Month.January; // First month of the year is January
Console.WriteLine(firstMonth); // Prints out `January`
firstMonth = "Hello World!"; // Errors out!
}
}
This is mostly used to help us read and write our code. It gets rid of magic numbers, wherein you assign a number to a status (much like error 404). It helps in removing the cognitive load of having to translate the number to something that's meaningful.
Resources to refer to
Blog post about Enums in general
C# documentation on Enums
Java documentation
Contributing
- Create the document using master/reference/concepts/enums.md
- Add a link to the reference document in the reference README at master/reference/concepts/README.md.
Help
If you have any questions while adding the reference document, please post the questions as comments in this issue.
This issue describes how to add a new reference document: <enums.md>.
Description
Using
enumsis a great way to limit the possible values of a variable. It allows you to create your own variable type and define its set of allowable values. You'd have to define theenumfirst with its possible values then, you can use the definedenumto give your variables that type. That variable can now only have possible values as defined by thatenumFor example, you're working with months, you would want to create an
enumcalled Month, and set its allowable values to the 12 possible months. Then you can create a variable of typeMonthand name it firstMonth. The compiler, or your tool will only allow you to setfirstMonthto one of the possible values.Here's an example using C#:
This is mostly used to help us read and write our code. It gets rid of magic numbers, wherein you assign a number to a status (much like error 404). It helps in removing the cognitive load of having to translate the number to something that's meaningful.
Resources to refer to
Blog post about Enums in general
C# documentation on Enums
Java documentation
Contributing
Help
If you have any questions while adding the reference document, please post the questions as comments in this issue.