I have written an age calculator that takes a birthDate as input.I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try/catch statement.namespace Age{    class Program    {        static void Main(string[] args)        {            while (true)            {                try                {                    Console.Write(Enter your birtdate: );                    DateTime birthDate = DateTime.Parse(Console.ReadLine());                    int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear);                    int Years = Days / 365;                    string message = (Days >= 365) ? Your age:  + Years +  years : Your age:  + Days +  days;                    Console.WriteLine(message);                }                catch                {                    Console.WriteLine(You have entered an invalid date.\n);                }                Console.WriteLine(Exit? (y/n));                string userValue = Console.ReadLine();                if (userValue == y)                {                    Environment.Exit(0);                }            }        }    }}
Simple Age Calculator
c#;beginner;datetime
You should give the user some format to follow when entering their birthdayConsole.Write(Enter your birthdate (MM/DD/YYYY): );You should give the user a way to close the program. While this isn't incredibly necessary for simple learning programs, it is a good habit to get into.You should avoid throwing exceptions, they break up your program's flow. Certainly use them where necessary, but this is not one of those places. Exceptions should be used in situations when your function doesn't have the ability to handle a situation/error.In the following snippet, I've declared the DateTime object, and a Boolean which will tell us if the string entered by the user was able to be parsed. Notice how I am calling the TryParse Method. The TryParse method takes in a string, and an out DateTime object. This is important, the out keyword is a bit more advanced than you have come accross. It is a keyword which implements functionality programmers were used to in C using pointers. Suffice to say that this function will set whatever object you pass in, to be the parsed value. DateTime birthDate;bool succeeded = DateTime.TryParse(Console.ReadLine(), out birthDate);Because the TryParse method returns a bool you can just use this in an if statement which is conditional on the TryParse returning true.if(DateTime.TryParse(Console.ReadLine(), out birthDate)){    // more stuff}else    Console.WriteLine(You have entered an invalid date.);I noticed you used \n in your program. While I will say that I am guilty of using this all the time, you should actually use Environment.NewLine. The reason is because of in-consistency of newline/carriage returning in different programs. Environment.NewLine always uses the proper line ending. Console.WriteLine(You have entered an invalid date. + Environment.NewLine);You can use + or - operators on DateTime objects which results in a TimeSpan objectTimeSpan age = DateTime.Now - birthDate;TimeSpan objects have properties like Days, Hours. the calculation done, does take into consideration leap years.Printing to the console can be made easier by sending parameters to be inserted into your string.Console.WriteLine(Your age: {0} years and {1} days, (int)(age.Days/365.25), age.Days % 365.25);Over all, your program could look like this. Note: I didn't programatically address the issue of the user escaping your program, I noticed you updated your OP with that.while (true){    Console.Write(Enter your birtdate (MM/DD/YYYY): );    DateTime birthDate;    if (DateTime.TryParse(Console.ReadLine(), out birthDate))    {        TimeSpan age = DateTime.Now - birthDate;        Console.WriteLine(Your age: {0} years and {1} days, (int)(age.Days/365.25), age.Days % 365.25);    }    else        Console.WriteLine(You have entered an invalid date. + Environment.NewLine);}Edit: This is of-course personal taste, but I dislike (what I consider to be unnessary brackets taking up newlines).else{    Console.WriteLine(You have entered an invalid date. + Environment.NewLine);}Some people prefer that, I just am not, and that is up to every coder to decide. If I was to put brackets arround that, I would probably do it this wayelse    { Console.WriteLine(You have entered an invalid date. + Environment.NewLine); }