代码清单4-4 Person类的部分代码,其中包含了年龄的计算

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Person turing = new Person("Alan Turing", new DateTime(1912, 6, 3), new DateTime(1954, 6, 7));
            Person knuth = new Person("Donald Knuth", new DateTime(1938, 1, 10), null);
            Console.ReadKey();
        }

        class Person
        {
            DateTime birth;
            DateTime? death;
            string name;
            public TimeSpan Age
            {
                get
                {
                    if (death == null)
                    {
                        return DateTime.Now - birth;
                    }
                    else
                    {
                        return death.Value - birth;
                    }
                }
            }

            public Person(string name,DateTime birth, DateTime? death)
            {
                this.birth = birth;
                this.death = death;
                this.name = name;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/liuslayer/p/6963947.html