How to assign value to DateTime? field of object.

Jul 22 2019 11:56 PM
I have declared Person class as below.
 
public class Person
{
public string Prefix { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
}
 
I create an Person object.
 
Person _person = new Person { Prefix = "Mr", FirstName = "William", LastName = "Jonathan", DateOfBirth = DateTime.Now.AddYears(-29) }; 
 
Problem --> I got an error "Object reference not set to an instance of an object." in setting value to DateOfBirth.
 
Anybody help me? How to solved it?
 
Thank you.
Thakerng. 
 

Answers (4)