不能 link 两种方法 C#

Can't link two methods C#

我有代码:

using System;
using System.Globalization;
using System.IO;
using System.Linq;

namespace TimeTress
{
    class Program
    {
        static void Main(string[] args)
        {
            var timelineCSV = GetListOfStringsFromTextFile("../../../../timeline.csv");
            var peopleCSV = GetListOfStringsFromTextFile("../../../../people.csv");

            var timeline = ReadData(timelineCSV);
            var people = ReadData(peopleCSV);
        }
        static string GetNameOfPeoples(string[][] people)
        {
            var nowDate = DateTime.Now;
            var countYears = 20;
            if ((DateTime.IsLeapYear(yearBirthday)) && (bitrthdayElement.Year - nowDate.Year) <= countYears) return namePeople;
        }
        static (string, int, DateTime) GetNamePeoplesAndYears(string[][] people)
        {
            foreach (var elements in people)
            {
                DateTime bitrthdayElement = ParseDate(elements[2]);
                var yearBirthday = bitrthdayElement.Year;
                var namePeople = elements[1];
            }
            return (namePeople, yearBirthday, birthdayElement);
        }
        static string[] GetListOfStringsFromTextFile(string filePath)
        {
            string[] stringArray;

            if (File.Exists(filePath))
            {
                stringArray = File.ReadAllLines(filePath);
            }
            else
            {
                stringArray = new string[0];
            }

            return stringArray;
        }
        static void OutputString(string[] getString)
        {
            foreach (var lines in getString)
            {
                Console.WriteLine(lines);
            }
        }

        static string[][] ReadData(string[] array)
        {
            string[][] splitData = new string[array.Length][];
            for (var i = 0; i < array.Length; i++)
            {
                var line = array[i];
                string[] parts = line.Split(";");
                splitData[i] = parts;
            }

            return splitData;
        }
        static DateTime ParseDate(string value)
        {
            DateTime date;
            if (!DateTime.TryParseExact(value, "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            {
                if (!DateTime.TryParseExact(value, "yyyy-mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    if (!DateTime.TryParseExact(value, "yyyy-mm-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                    {
                        throw new Exception("WRONG FORMAT");
                    }
                }
            }

            return date;
        }

    }
}

在 GetListOfStringsFromTextFile 中我得到字符串[]。 在 ReadData 中,我得到一个 string[][].

整个程序运行正常,问题出在两个方法上。

GetNamePeoplesAndYears 从 {1; 这样的文件中获取值姓名; date} 和 returns 三值姓名、出生年份和完整的出生日期

第二种方法GetNameOfPeoples检查出生年份是否为闰年,然后检查此人是否小于20岁

如果您想要 link GetNamePeoplesAndYears 和 GetNameOfPeoples 这两个方法和 return 您可能需要更改传递给 GetNameOfPeoples 函数的参数的值。通过传递相应的值或具有所需数据的 class 可以解决您的问题。例如:

 var result =  GetNamePeoplesAndYears(people);
 GetNameOfPeoples(result.Item1, result.Item2, result.Item3);


      static string GetNameOfPeoples(string namePeople, int yearBirthday, DateTime bitrthdayElement )
        {
            var nowDate = DateTime.Now;
            var countYears = 20;
            if ((DateTime.IsLeapYear(yearBirthday)) && (bitrthdayElement.Year - nowDate.Year) <= countYears) 
                return namePeople;
             return String.Empty; // if your data doesn't met the condion it needs to return some string. So add needful 

            }

通过使用 class 你可以做同样的事情。那么参数将是那个 class 的对象。 希望能帮到您解决这个问题。

编辑:如果你想要字符串[][]作为输入参数和return所有的名字和年龄你可以试试这个

static List<propleData> NamePeoples(string[][] people) {
            
        var listPeople = new List<propleData> { };
        foreach (var elements in people) 
        {
            var item = new propleData();
           
            item.names = elements[1];
            item.years = ParseDate(elements[2]).Year;
            listPeople.Add(item);
        }
        return listPeople;
   
}
//create a class or you can use dictionary instead of list
        public class propleData
        {
            public string names { get; set; }
            public int years { get; set; }


        }

您可以通过迭代列表从列表中检索数据。我举个例子。

var namePeople = NamePeoples(people);
            string name = string.Empty;
            int year = 0;
            foreach (propleData propleData in namePeople)
            {
                name = propleData.names;
                year = propleData.years;

                // you can print or save the data to array
            }