Считать из файла и записать в лист с несколькими переменными-Tuple

Нужно считать из текстового файла значения и записать их в лист, чтобы каждое значение записывалось в свою переменную. Например: "1 января", 1 записывается в int, а январь в string

Вот данные:

1 января -5 Пасмурно
2 января -2 Облачно
3 января -4 Пасмурно
4 января -1 Дождь
5 января -2 Пасмурно
6 января -5 Пасмурно
7 января -11 Пасмурно
8 января -9 Снег
9 января -12 Пасмурно
10 января -11 Пасмурно
11 января -6 Снег
12 января -1 Пасмурно
13 января -2 Пасмурно
14 января -1 Пасмурно
15 января +1 Дождь

Умею это делать на C просто в различные переменные, а как сделать на C# тем более в лист, вообще не понимаю. Прошу помочь

List<Tuple<int, string, int, string>> lines = new List<Tuple<int, string, int, string>>();
FileStream file = new FileStream("weather.txt", FileMode.Open);
            StreamReader readFile = new StreamReader(file);
            while (!readFile.EndOfStream)
            {
                lines.Add(new Tuple<int, string, int, string>(readFile.Read(), readFile.ReadLine(), readFile.Read(), readFile.ReadLine()));
            }
            readFile.Close();

Ответы (1 шт):

Автор решения: Александр ДД

Добавьте 2 класса:

1 класс (сущность):

public class WeatherInfo
{
    public WeatherInfo(DateTime date, int temperature, string weatherFeature)
    {
        Date = date;
        Temperature = temperature;
        WeatherFeature = weatherFeature ?? throw new ArgumentNullException(nameof(weatherFeature));
    }

    public DateTime Date { get; private set; }
    public int Temperature { get; private set; }
    public string WeatherFeature { get; private set; }
}

2 класс: (Using сам вызывает Dispose объектов в скобках, поэтому Close вызывать не надо.)

public static class WeatherWorker
{
    public static List<WeatherInfo> ParseWeatherInfoFromFile(string filePath)
    {
        List<WeatherInfo> weatherInfos = new List<WeatherInfo>();

        try
        {
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        string[] words = reader.ReadLine().Split(' ');

                        if (words.Length != 4)
                        {
                            throw new OperationCanceledException("Нарушение структуры исходного файла");
                        }

                        weatherInfos.Add(new WeatherInfo(DateTime.Parse(words[0] + "." + words[1]), Int32.Parse(words[2]), words[3]));
                    }
                }
            }

        }
        catch (OperationCanceledException exc)
        {
            Console.WriteLine(exc.Message);
        }
        catch (FileNotFoundException exc)
        {
            Console.WriteLine(exc.Message);
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.Message);
            // TODO: Обработать другие исключения.
        }

        return weatherInfos;
    }
}

Используйте так:

        List<WeatherInfo> weatherInfos = WeatherWorker.ParseWeatherInfoFromFile("D:\\weather.txt");

        if (weatherInfos.Any())
        {
            Console.WriteLine("Parsed lines:");
            weatherInfos.ForEach(wi => Console.WriteLine(
                "Day: " + wi.Date.Day + ", Month: " + wi.Date.Month + ", Temperature: " + wi.Temperature + ", Feature: " + wi.WeatherFeature));
        }

        Console.ReadLine();
→ Ссылка