结构练习中的问题

本文关键字:问题 练习 结构 | 更新日期: 2023-09-27 18:00:36

我目前正在学习结构,所以我有以下练习:设置一个名为"Date"的结构,该结构包含日期,包括:年、月和日。另外,定义一个名为Phone的类,该类包含名称、号码、出生日期和地址。您需要创建一个包含Phone类型对象的数组,并按名称、编号和日期对其进行排序。好的,这就是代码:

  struct Date
{
    int year, month, day;
    public Date(int year, int month, int day)
    {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public int Year
    {
        get { return year; }
        set {year = value; }
    }
    public int Month
    {
        get { return month; }
        set { month = value; }
    }
    public int Day
    {
        get { return day; }
        set { day = value; }
    }
}
    class Phone
{
    string number;
    string adress; 
    string name;
    Date birthday = new Date(); 
    public Phone(string number,Date birthday, string adress, string name)
    {
        this.number = number;
        this.birthday = birthday;
        this.adress = adress;
        this.name = name;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Phone[] p = new Phone[3];
        p[0] = new Phone(1072548,
     }
}

我没有错,但问题是我不知道如何从"Date"结构中获取生日,这就是我停止输入信息的原因。谢谢

结构练习中的问题

p[0] = new Phone(1072548,
   new Date (1999, 12, 31),
   "Central Park, NY", "Sam Party")

也许可以将其添加到您的日期结构中:

public DateTime ToDateTime ()
{
   return new DateTime (Year, Month, Day);
}

然后,你可以这样排序你的数组:

array.OrderBy (p => p.BirthDate.ToDateTime ());
struct Date
{
    Date()
    {
       //code
    }
}
class Phone
{
    Phone(string "someParametr")
    {
      //code
    }
}

你需要两个承包商。