Possible error in C# compiler warning output for nullability checking

Type type = typeof(Program);
type = type.BaseType; // [CS8600] Converting null literal or possible null value to non-nullable type.
    
do
{
    type = type.BaseType; // [CS8602] Dereference of a possibly null reference. + [CS8600] Converting null literal or possible null value to non-nullable type.
}
while(true);

In the program above I would expect the same warning from the compiler (BaseType can be null so it cannot be assigned to type variable). While in reality in the second case I get another error that seems wrong to me (type variable cannot be null by definition).

Is this some kind of an error in compiler, or the compiler is on the contrary is “extra smart” and figures out that I do try to possibly assign null to non-nullable variable and since it is only a warning, it will now show both warnings?

If I comment the first assignment, the result is the same.

1

The compiler is indeed “extra smart” in this case.

For the line type = type.BaseType; in the loop, It sees that you are assigning BaseType (possibly null) to a variable type that is not supposed to be null. This produces CS8600, for the same reason as the first instance of CS8600.

It also sees that the expression type in type.BaseType is possibly null. This is because the compiler can see that you just assigned a possibly-null thing to type. Therefore, type.BaseType is dereferencing a possibly-null thing, and might throw a NullReferenceException. This causes CS8602.

The “null state” of every variable is tracked throughout the method. For more information on how the compiler does null-tracking, see the documentation here. Here I have annotated the null-state of type at each line of the program:

Type type = typeof(Program);

// not null, because typeof(Program) never produces null

type = type.BaseType;

// maybe null, because BaseType is of type 'Type?'
    
do
{
    // still maybe null here

    type = type.BaseType;

    // still maybe null here
}
while(true);

// not null, because this line is unreachable :)

The result is the same even when you remove the line type = type.BaseType; that is outside the loop, because it’s a loop. Even if type will not be null in the first iteration, it is still possibly null in the second iteration.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật