using System;
using System.Diagnostics;
namespace pc.AssertsExample
{
class Program
{
public class Person
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public Person(string Name, DateTime BirthDate)
{
this.Name = Name;
this.BirthDate = BirthDate;
}
public int GetAge()
{
DateTime today = DateTime.Today;
int age = today.Year - BirthDate.Year;
return BirthDate > today.AddYears(-age) ? --age : age;
}
public override string ToString()
{
return Name + " is " + GetAge();
}
}
static void Main(string[] args)
{
var pawel = new Person("Pawel", new DateTime(1984, 1, 31));
Debug.Assert(pawel.GetAge() == 30, "Pawel isn't 30");
}
}
}
Running the above will result in the following message box
We have three options:
- Abort: the program ends
- Retry: you step into the code at the assert statement.
- Ignore: continues on