Click here to Skip to main content
15,124,055 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
JokeRe: C# 9 nullable reference alert Pin
David O'Neil29-Sep-21 21:35
professionalDavid O'Neil29-Sep-21 21:35 
GeneralRe: C# 9 nullable reference alert Pin
Richard Deeming29-Sep-21 23:02
mveRichard Deeming29-Sep-21 23:02 
GeneralRe: C# 9 nullable reference alert Pin
Member 916705730-Sep-21 21:39
MemberMember 916705730-Sep-21 21:39 
GeneralRe: C# 9 nullable reference alert Pin
ZevSpitz30-Sep-21 23:45
professionalZevSpitz30-Sep-21 23:45 
GeneralRe: C# 9 nullable reference alert Pin
Super Lloyd1-Oct-21 0:30
MemberSuper Lloyd1-Oct-21 0:30 
GeneralRe: C# 9 nullable reference alert Pin
obermd1-Oct-21 4:26
Memberobermd1-Oct-21 4:26 
GeneralRe: C# 9 nullable reference alert Pin
Al Gonzalez1-Oct-21 5:10
MemberAl Gonzalez1-Oct-21 5:10 
GeneralRe: C# 9 nullable reference alert Pin
Adam David Hill6-Oct-21 11:42
professionalAdam David Hill6-Oct-21 11:42 
Wow, really interesting example. My personal experience has been really positive with Null Reference Types, and it's caught many real world issues long before production, but good to have a cold reminder.

I guess I normally consider the point of NRT for me is that it increases the chance that the compiler at least puts the developer to the decision, so if it's a mistake it's a conscious one. This is a good example where that can't be guaranteed.

Let me try a counter argument to the IList example, if not only for my own sanity and shrinking NRT world view... So, the null was permitted, not because it was added to a collection of non-nullable strings, but effectively it was added to a list of explicitly nullable objects. Yes, explicitly is not so explicit yet, but hear me out. If you now try and pluck that null object out, and use it where a null is not permitted, I believe the compiler will realise, as your IList is actually IList<object?>, so the explicit typing is actually there, and so the compiler will save you at the last minute before the null actually becomes a problem. Based on this I think the behaviour we see is ok and probably by design.

C#
// Warning is generated when you attempt to use it, because underlying type of `object?` is recognised
string s = ilist[0];


As for the array, though...? B*gger, that's scary. Maybe a bug? Weirdly it recognises array as being string[]? after the line it's declared on, but then still does not generate a warning when reading that string? back into a new string.

EDIT: Just realised your Console.Writeline bits do demonstrate you reading it out. Are there any examples that don't involve array [] syntax? Maybe it's a bug worth raising?

modified 6-Oct-21 16:48pm.

GeneralRe: C# 9 nullable reference alert Pin
Super Lloyd6-Oct-21 14:23
MemberSuper Lloyd6-Oct-21 14:23 
JokeFound your next laptop Pin
raddevus29-Sep-21 11:56
mvaraddevus29-Sep-21 11:56 
GeneralRe: Found your next laptop Pin
Cp-Coder29-Sep-21 12:22
MemberCp-Coder29-Sep-21 12:22 
GeneralRe: Found your next laptop Pin
Mike Hankey29-Sep-21 12:39
professionalMike Hankey29-Sep-21 12:39 
GeneralRe: Found your next laptop Pin
Maximilien29-Sep-21 12:59
MemberMaximilien29-Sep-21 12:59 
GeneralRe: Found your next laptop Pin
raddevus29-Sep-21 13:04
mvaraddevus29-Sep-21 13:04 
GeneralRe: Found your next laptop Pin
David O'Neil29-Sep-21 13:12
professionalDavid O'Neil29-Sep-21 13:12 
GeneralRe: Found your next laptop Pin
Duke Carey30-Sep-21 1:26
professionalDuke Carey30-Sep-21 1:26 
GeneralRe: Found your next laptop Pin
Jon McKee29-Sep-21 13:29
professionalJon McKee29-Sep-21 13:29 
GeneralRe: Found your next laptop Pin
Richard Andrew x6429-Sep-21 14:31
professionalRichard Andrew x6429-Sep-21 14:31 
GeneralRe: Found your next laptop Pin
JohaViss611-Oct-21 2:09
professionalJohaViss611-Oct-21 2:09 
GeneralRe: Found your next laptop Pin
den2k8829-Sep-21 22:18
professionalden2k8829-Sep-21 22:18 
GeneralRe: Found your next laptop Pin
User 991608030-Sep-21 5:00
professionalUser 991608030-Sep-21 5:00 
GeneralRe: Found your next laptop Pin
dandy7230-Sep-21 6:28
Memberdandy7230-Sep-21 6:28 
GeneralRe: Found your next laptop Pin
den2k8830-Sep-21 22:37
professionalden2k8830-Sep-21 22:37 
GeneralRe: Found your next laptop Pin
dandy721-Oct-21 4:06
Memberdandy721-Oct-21 4:06 
GeneralRe: Found your next laptop Pin
Slow Eddie1-Oct-21 3:23
professionalSlow Eddie1-Oct-21 3:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.