C# Program to find Age

Below is a C# program to find Age from a Date of Birth. Enjoy!

public class Person
   {
       public string Name { getset; }
 
       public Person(DateTime birthdate)
       {
           BirthDate = birthdate;
       }
       public DateTime BirthDate { getprivate set; }
 
       public int Age{
           
           get { 
 
               var timeSpan = DateTime.Today - BirthDate;
               var years = timeSpan.Days / 365;
 
               return years;                
           
           }
       }
 
   }
 
   public class Program
   {
       public static void Main(string[] argvs)
       {
           var person = new Person(new DateTime(1998,2,14));
           
           Console.WriteLine(person.Age);
       }
   }

Add a Comment

Your email address will not be published. Required fields are marked *