Enums and struct on c#

Interesting that c# has structs, the first time i encountered it was in c++

In this example I will be messing about with the enum, defining directions with Int as values. I am still getting around the enums. I have seen its use in different languages but not really used it.

I’ve setup the following enum and struct in the header like so

namespace ConsoleApplication1
{
    enum Oritentation : int
    {
        North = 12,
        South = 1233,
        West = 33
    }

    struct route
    {
        public route(Oritentation direction, double distance)
        {
            this.direction = direction;
            this.distance = distance;
        }

        public Oritentation direction;
        public double distance;
    }

    class Program
    { ...}
....
}

Then I called and used the enums and structs like so:

static void Main(string[] args)
        {
            //declare the direction var as type Orientation
            Oritentation direction;
            //assign only valid value of Orientation.North
            direction = Oritentation.North;

            //convert value direction North to Int
            int intdirection = (int) direction;

            //output the int
            WriteLine($"Orientation North has the value of {intdirection}");
            WriteLine($"Direction has the value of {direction}");


            route coordinate = new route(Oritentation.North, 23.9);

            WriteLine(coordinate.direction);

            ReadKey();
        }

An update on the learning so far I have completed the majority of the basics, such as variables, loops, datatypes, I will soon be looking at functions and classes, which would be needed as I understand there are different accessibility types for the functions, and classes.

I am also interested to see how inheritance is use.

One thing I’ve found is that in the switch cases you can use when to have further logic process in the switch case.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.