If you check an object for its type and find out it is of the type you checked for, and you convert it to this type, can this type still be null after conversion ?
The code quality scanning app I'm running is complaining about the following:
if (tx.Tag is ExtendedNodeInfo && ty.Tag is ExtendedNodeInfo)
{
var tagX = tx.Tag as ExtendedNodeInfo;
var tagY = ty.Tag as ExtendedNodeInfo;
// HP Fortify scan says the below line's use of tagX/Y can be null.
// If I add null checks below for taX/Y, Resharper says
// its redundant as its always not null
return tagX.Ordinal.CompareTo(tagY.Ordinal);
}
If tx.Tag
doesn't change its value, that's fine - but presumably the code quality scanner doesn't know that. It's generally better to use as
for this, so that you only evaluate the property once, only perform the type test once, and then just check the references:
var tagX = tx.Tag as ExtendedNodeInfo;
var tagY = ty.Tag as ExtendedNodeInfo;
if (tagX != null && tagY != null)
{
return tagX.Ordinal.CompareTo(tagY.Ordinal);
}
See more on this question at Stackoverflow