Home

C# Enum Including Loop Through Absolute Basics

The Code

using System.Text;
using System.Net;

namespace Test
{
    public class Program
    {
        public static Boolean Loop = true;

        enum Marques { Ford, Vauxhall, BMW, Mercedes, Citroen, Seat, Audi };

        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine(Marques.Audi);
                Console.WriteLine(Marques.Ford);

                Console.WriteLine("==================");

                foreach(string x in Enum.GetNames(typeof(Marques)))
                {
                    Console.WriteLine(x);
                }

                Console.Read();
            } while (Loop == true);
        }
    }
}

The Explanation

I'm not quite sure of the puprose of enums yet. They appear on first viewing just a quick-n-dirty way to store a bit of data in the code that you can access as you see fit. In the above example to use the string Audi I could type Marques.Audi. I suppose if I want to be SURE that I'm only using what is in the enum list then this is a good way to avoid spelling mistakes.

I have much to learn and at some point I may come across a better, more logical use for enums... but I'm not there yet.

Of note - when declaring the enum the "e" is lower case. When using the Enum.GetNames note the capital "E". I believe there is a convention at work here, not sure what it is as yet...

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog