You could use an array for this:
string[] daysOfWeek = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int dayVal = 2;
if (dayVal >= 0 && dayVal < daysOfWeek.Length) // check it's within the bounds of the array, we don't want any IndexOutOfBounds exceptions being thrown for bad user input
{
Console.WriteLine($"Today is {daysOfWeek[dayVal]}.")
}
Alternatively, you could use a dictionary:
Dictionary<int, string> daysOfWeek = new Dictionary<int, string>()
{
{ 0, "Sunday" },
{ 1, "Monday" },
{ 2, "Tuesday" },
{ 3, "Wednesday" },
{ 4, "Thursday" },
{ 5, "Friday" },
{ 6, "Saturday" }
};
int dayVal = 2;
if (daysOfWeek.TryGetValue(dayVal, out string dayName)) // check it's in the dictionary and retrieve the result
{
Console.WriteLine($"Today is {dayName}.");
}
Or you could create an enum (for days of the week you don't need to since .NET already has such an enum):
public enum DaysOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
int dayVal = 2;
if (Enum.IsDefined(typeof(DaysOfWeek), dayVal)) // check it's in the enum
{
var dayOfWeek = (DaysOfWeek)dayVal;
Console.WriteLine($"Today is {dayOfWeek}."); // this will print the name corresponding to the enum value
}
And to instead use the built-in .NET DayOfWeek enum:
int dayVal = 2;
if (Enum.IsDefined(typeof(DayOfWeek), dayVal)) // check it's in the enum
{
var dayOfWeek = (DayOfWeek)dayVal;
Console.WriteLine($"Today is {dayOfWeek}."); // this will print the name corresponding to the enum value
}
To determine the next day of the week you should use the following:
int nextDay = dayVal == 6 ? 0 : dayVal + 1;
Note that an alternative approach is the following:
DateTime today = DateTime.Now.Date;
Console.WriteLine($"Today is {today.DayOfWeek}.");
DateTime tomorrow = today.AddDays(1);
Console.WriteLine($"Tomorrow is {tomorrow.DayOfWeek}.");