Я новичок в программировании.Теперь я должен изучить элементы списка C #.
Мои ожидания:
- Создать пустой список
- Получить значение ввода для этого спискаот пользователя через окно консоли.
Моя программа:
using System;
using System.Collections.Generic;
namespace Indexof_in_List
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Implementation of IndexOf Method with List");
Console.WriteLine();
//If the user entered empty value or nothing in the console window, the window will exit automatically
//Create an Empty List
List<int> name = new List<int>();
Console.WriteLine("Enter the Input values for the List");
//Get the Inputs for the List from the Console Window
for (int n = 0; n < name.Count; n++)
{
bool check;
string input = Console.ReadLine();
check = int.TryParse(input, out int val);
if (check)
{
name[n] = val;
}
else
{
Environment.Exit(0);
}
}
//Implement Index of any number in the list items which you have entered to the console
Console.WriteLine("The Index of value 10 is = {0}",name.IndexOf(10));
Console.WriteLine();
Console.WriteLine("The Index of value 1000 is ={0}", name.IndexOf(1000));
Console.WriteLine();
Console.ReadKey();
}
}
}
Вывод: ![Console Output](https://i.stack.imgur.com/y0sut.png)
Может кто-нибудь сказать мне решение для этой логики C #?А также скажите мне причину этого сбоя?
И скажите, правильна моя логика или нет?