Skip to content

Chap 06: Comparing objects #93

@Strydir

Description

@Strydir

Page 235:
the line "int result = x.Name.Length.CompareTo(y.Name.Length) results in the errors "'Name may be null here. Dereference of a possibly null reference".

To fix it, you must test to make sure that Name is not null first:

if((x is not null) && (y is not null))
{
if(x.Name is null) return 1;
if(y.Name is null) return -1;
int result = x.Name.Length.CompareTo(y.Name.Length);
}

Obviously, the errors disappear if you modify the file according to the "errata" for null values for page 235, but, as printed, the text does not work.
A possible VS Code version issue? Using VS Code version 1.69.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions