I'm beginner in programming and I have tried some simple code in C#. [1]: https://i.sstatic.net/zLVbz.jpg This code deals with simple array initialization ,storing and sorting and so on.Its runs as usual in the first try but when I again want to store in the array it throws the exception which I don't understand. [![Its the exception I'm getting][1]][1]
static void Main(string[] args)
{
int number;
char y;
string[] answer = new string[10];
bool keeprompting = true;
while (keeprompting)
{
Console.WriteLine("Enter the options given below 1.Add students\n 2.View all details\n 3.Sorting\n 4.Exit\n");
int input = Convert.ToInt16(Console.ReadLine());
switch (input) {
case 1:
Console.WriteLine("Enter the Number of Students to be added to the List");
number = Convert.ToInt16(Console.ReadLine()); **exception **
for (int i = 0; i < number; i++) {
answer[i] = Console.ReadLine();
}
break;
case 2:
foreach(var item in answer)
{
Console.WriteLine(item.ToString());
}
break;
case 3:
Array.Sort(answer);
foreach(var item in answer)
{
Console.WriteLine(item.ToString()); **exception **
}
break;
case 4:
Console.WriteLine("Are you sure you want to exit");
Console.WriteLine("1 for Yes and for No");
y = (char) Console.Read();
if (y != 1) {
keeprompting = true;
} else {
keeprompting = false;
}
Console.WriteLine("thank you");
break;
}
}
}
Any and all suggestion are welcome.
Int16.TryParse.