C# noob here, trying to experiment with different ways of solving a basic problem. I want to pass in an argument to a method and in that method I loop through an array of months. If the argument equals the position of the array, I want to return the string of that array position.
I've tried the following:
class Month
{
private int month;
public string strMonth(int month)
{
this.month = month;
string[] months = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
for (int i = 0; i < months.Length; i++)
{
if (month == Array.IndexOf(months, i))
{
return months[i];
}
}
return "check fails";
}
}
And for my driver I'm using
class Program
{
static void Main(string[] args)
{
Month testMonth = new Month();
Console.WriteLine(testMonth.strMonth(2));
Console.ReadKey();
}
}
However, I'm always getting check fails
logged in the console. Am I on the right path or has the noobness prevailed and I'm doing this totally wrong? I'm also confused about the block level scoping (I think that's what C# does?). I come from a JS background and I'm used to function level scope. Will adding the return "check fails"
always execute even if my check passes?