C# 检查对象的每个级别是否存在 让我们考虑下面的一个示例模型。 class student { public int ID { get; set; } public string Name { get; set; } public school SchoolName { get; set; } } class school { public int ID { get; set; } public string Name { get; set; } } student student1 = new student(); Console.WriteLine(student1.SchoolName.Name);

C# 检查对象的每个级别是否存在 让我们考虑下面的一个示例模型。 class student { public int ID { get; set; } public string Name { get; set; } public school SchoolName { get; set; } } class school { public int ID { get; set; } public string Name { get; set; } } student student1 = new student(); Console.WriteLine(student1.SchoolName.Name);,c#,C#,正如我们所知,我们访问学校名称如下 class student { public int ID { get; set; } public string Name { get; set; } public school SchoolName { get; set; } } class school { public int ID { get; set; } public string Name { get; set; } } student studen

正如我们所知,我们访问学校名称如下

class student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public school SchoolName { get; set; }
}

class school
{
    public int ID { get; set; }
    public string Name { get; set; }
}

student student1 = new student();
Console.WriteLine(student1.SchoolName.Name);
如果未将
学校
分配给
学生
学生1.学校名
将为空。因此,上面的
Console.WriteLine()
失败

我最后为所有这些元素编写了一个
if
语句,这是a$$中的一个难题。我们有其他方法来处理这些情况吗?

试试这个:

Console.WriteLine(student1.SchoolName?.Name);

如果SchoolName为null,则不会计算Name属性。

不幸的是,我们必须执行显式
null
检查以前版本的
C

C#
6.0
中,我们有操作符

该操作符的作用与您所看到的完全相同,如果操作符的左侧为null,它会将结果类型的null级联到右侧

所以你可以这样做

Console.WriteLine(student1.SchoolName?.Name);
简言之,如果将
替换为null条件运算符
?。
它会将null逐行级联:

在旧版本的C#(在C#6之前)中选中此项,您可以尝试使用条件运算符:

Console.WriteLine(student1.SchoolName !=null? student1.SchoolName.Name:string.empty);

您没有其他选择:

1.如下定义属性
SchoolName

private school _SchoolName;
public school SchoolName
{
    get
    {
        if (_SchoolName == null)
            return new school() { ID = 0, Name = string.empty };
        else
            return _SchoolName;
    }
    set { _SchoolName = value; }
}
class student
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    [ForeignKey(...)]
    public virtual school School { get; set; }
}

class school
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual List<student> Students {get; set; }
}

student student1 = new student();
2.在打印值之前检查空值

Console.WriteLine(student1.SchoolName ==null?string.empty: student1.SchoolName.Name);

如果您使用的是6.0之前的C#版本,那么您可以使用:

或者,一个`'也可以做到这一点:

Console.WriteLine(student1.SchoolName?.Name);

最佳做法如下:

private school _SchoolName;
public school SchoolName
{
    get
    {
        if (_SchoolName == null)
            return new school() { ID = 0, Name = string.empty };
        else
            return _SchoolName;
    }
    set { _SchoolName = value; }
}
class student
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    [ForeignKey(...)]
    public virtual school School { get; set; }
}

class school
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual List<student> Students {get; set; }
}

student student1 = new student();
班级学生
{
[关键]
公共int ID{get;set;}
公共字符串名称{get;set;}
[外键(…)]
公共虚拟学校{get;set;}
}
班级学校
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟列表学生{get;set;}
}
学生1=新学生();

对于不处理空值的方法,可以将其扩展到student1.SchoolName?.Name??defaultvalue注意,这是一种C#6.0语法,OP可能使用的是旧版本。请尝试以下操作:Console.WriteLine(student1.SchoolName==null)?“”:student1.SchoolName.Name;在C#6.0之前的Console.WriteLine(student1.SchoolName==null)中尝试使用Roslyn?“”:student1.SchoolName.name谢谢大家。在C#6.0中工作没问题,很高兴它帮助了你:-)。三元运算符是一种if。是的,但使用if编写多行和块,只需在一行中编写即可。
Console.WriteLine(student1.School!=null?(student1.School.Name!=null?student1.School.Name:“n/a”):“n/a”)您在回答中提到了一些属性。你能用一下吗