我有以下代码:
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
var obj=(person)Activator.CreateInstance(typeof(person));
Console.WriteLine(obj);
}
}
public class person
{
public int id { get; set; }
public string name { get; set; }
public DateTime dob { get; set; }
public override string ToString()
{
return id.ToString() + " " + name + " " + dob.ToString();
}
}
}
产生以下输出:
0 1/1/0001 12:00:00 AM
但是,如果将其更改person.ToString()
为以下内容:
public override string ToString()
{
return id.ToString() + " " + name.ToString() + " " + dob.ToString();
}
我收到以下错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at Rextester.person.ToString()
有人可以对此有所了解。
已编辑