Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

[Reference] Add new reference document: enums.md #699

@batibot323

Description

@batibot323

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions