SaurabhCooks
SaurabhCooks

Reputation: 179

Print weekdays from enum

Being new to programming my question might seem a little basic one, what I want to is to print all the days mentioned in the enum using a loop or otherwise. I have used a console application for the same. Tips to improve basics of C# language coding capabilities along with the answer will be much appreciated.

using System;

namespace _28_11_2016_enum
{
class Program
{
    static void Main(string[] args)
    {
        weekdays wd = weekdays.mon;

        for (int i = 0; i < 7; i++)
        {


            int a = (int)wd;
            a = a + i;
            wd = (wd)a;// faulty code.
            Console.WriteLine(wd);
        }
        Console.Read();
    }
    enum weekdays : int
    {
        mon, tue, wed, thur, fri, sat, sun
    }
}

}

Upvotes: 3

Views: 3444

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186698

You don't have to loop - Enum.GetNames returns the names, and string.Join concat them together into a single string:

 // mon, tue, wed, thur, fri, sat, sun
 Console.Write(string.Join(", ", Enum.GetNames(typeof(weekdays))));

in case you want int values:

 // 0, 1, 2, 3, 4, 5, 6
 Console.Write(string.Join(", ", Enum.GetValues(typeof(weekdays)).Cast<int>()));

Edit: if you insist on loop I suggest foreach one:

 // mon == 0 ... sun == 6
 foreach (var item in Enum.GetValues(typeof(weekdays))) {
   Console.WriteLine($"{item} == {(int) item}"); 
 }

In case of for loop

 // do not use magic numbers - 0..7 but actual values weekdays.mon..weekdays.sun 
 for (weekdays item = weekdays.mon; item <= weekdays.sun; ++item) {
   Console.WriteLine($"{item} == {(int) item}"); 
 } 

However, in real world applications, please, use standard DayOfWeek enum.

Edit 2: your own code (in the question) improved:

 static void Main(string[] args) {
   for (int i = 0; i < 7; i++) { // do not use magic numbers: what does, say, 5 stand for?
     // we want weekdays, not int to be printed out
     weekdays wd = (weekdays) i;

     Console.WriteLine(wd);
   }

   Console.Read();
 }

Edit 3: your own code (in the answer) improved:

 // you have to do it just once, that's why pull the line off the loop 
 string[] s = Enum.GetNames(typeof(weekdays));

 // do not use magic numbers - 7: what does "7" stands for? - 
 // but actual value: s.Length - we want to print out all the items
 // of "s", i.e. from 0 up to s.Length    
 for (int i = 0; i < s.Length; i++)
   Console.WriteLine(s[i]);

Upvotes: 2

SaurabhCooks
SaurabhCooks

Reputation: 179

I have reworked the first code snippet using loops as shown by Dmitry

for (int i = 0; i < 7; i++)
        {

            string[]s = Enum.GetNames(typeof(weekdays));
            Console.WriteLine(s[i]);
        }

Upvotes: 0

user622505
user622505

Reputation: 773

You can simply do:

foreach (var weekDay in Enum.GetNames(typeof(weekDays)))
{
    Console.WriteLine(weekDay);
}

While we're at it, let me give you a few tips for naming/style conventions in C#.

enum's name should be using PascalCase:

enum WeekDays : int

Also in general you want to call your enum with a singular form:

enum WeekDay : int

Deriving from int is also superfluous:

enum WeekDay

enum member names should also be written with PascalCase. To increase readability, you may want to use full names instead of shorthands and write them in separate lines:

enum WeekDay
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

Of course, if you decide to follow these, then the first snippet becomes:

foreach (var weekDay in Enum.GetNames(typeof(WeekDay)))
{
    Console.WriteLine(weekDay);
}

Upvotes: 1

Hatted Rooster
Hatted Rooster

Reputation: 36483

wd = (wd)a;// faulty code.

The syntax of a cast is (Type)variable. So it should be

wd = (weekdays)a;

Upvotes: 1

Related Questions