我有以下代碼:檢查Null值的對象
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address address { get; set; }
public Person GetFullName()
{
return new Person { };
}
}
public class Address
{
public int Name { get; set; }
}
我需要檢查,如果人不爲空並且還包含了地址。對於這個下面的代碼工作:
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.FirstName="bla";
if (person != null && person.address != null)
{
Console.WriteLine(person.address.Name);
}
}
}
問我有:
如何爲person.Address執行該代碼應該扔空異常?
if (person != null && person.address != null)
感謝您的回答,它的乾淨。 – 2012-07-18 11:06:10