3

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
    }
}

}

SaurabhCooks
  • 179
  • 1
  • 9

4 Answers4

2

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]);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Wonderful, I love your answer. But as I am learning to code, I am trying to mess up with looping. Please can you help me get the answer through looping also? – SaurabhCooks Oct 28 '16 at 14:25
  • I like your slick coding style but I am quite new to coding and am pretty much getting overwhelmed by the above code. If you don't mind can you somehow get down to my level for sometime and improve my code itself so that I am able to follow through easily. – SaurabhCooks Oct 28 '16 at 14:39
  • You are a genius! I have the required output. Thanks a lot! – SaurabhCooks Oct 28 '16 at 14:52
  • Hi Dmitry, I have reworked the first code snippet using loops, plz check and comment: http://stackoverflow.com/a/40316810/7045194 – SaurabhCooks Oct 29 '16 at 07:01
1
wd = (wd)a;// faulty code.

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

wd = (weekdays)a;
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

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);
}
user622505
  • 743
  • 6
  • 23
0

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]);
        }
Community
  • 1
  • 1
SaurabhCooks
  • 179
  • 1
  • 9
  • 1
    push `string[]s = Enum.GetNames(typeof(weekdays));` off the loop and do not use *magic numbers*, but `s.Length` instead: `string[] s = Enum.GetNames(typeof(weekdays)); for (int i = 0; i < s.Length; ++i) Console.WriteLine(s[i]);` – Dmitry Bychenko Oct 29 '16 at 18:55