Click here to Skip to main content
15,891,473 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 20:35
professionalDavid O'Neil29-Sep-21 20:35 
GeneralRe: C# 9 nullable reference alert Pin
Richard Deeming29-Sep-21 22:02
mveRichard Deeming29-Sep-21 22:02 
GeneralRe: C# 9 nullable reference alert Pin
Member 916705730-Sep-21 20:39
Member 916705730-Sep-21 20:39 
GeneralRe: C# 9 nullable reference alert Pin
ZevSpitz30-Sep-21 22:45
professionalZevSpitz30-Sep-21 22:45 
GeneralRe: C# 9 nullable reference alert Pin
Super Lloyd30-Sep-21 23:30
Super Lloyd30-Sep-21 23:30 
GeneralRe: C# 9 nullable reference alert Pin
obermd1-Oct-21 3:26
obermd1-Oct-21 3:26 
GeneralRe: C# 9 nullable reference alert Pin
Al Gonzalez1-Oct-21 4:10
Al Gonzalez1-Oct-21 4:10 
GeneralRe: C# 9 nullable reference alert Pin
Adam David Hill6-Oct-21 10:42
professionalAdam David Hill6-Oct-21 10: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 13:23
Super Lloyd6-Oct-21 13:23 
JokeFound your next laptop Pin
raddevus29-Sep-21 10:56
mvaraddevus29-Sep-21 10:56 
GeneralRe: Found your next laptop Pin
Cp-Coder29-Sep-21 11:22
Cp-Coder29-Sep-21 11:22 
GeneralRe: Found your next laptop Pin
Mike Hankey29-Sep-21 11:39
mveMike Hankey29-Sep-21 11:39 
GeneralRe: Found your next laptop Pin
Maximilien29-Sep-21 11:59
Maximilien29-Sep-21 11:59 
GeneralRe: Found your next laptop Pin
raddevus29-Sep-21 12:04
mvaraddevus29-Sep-21 12:04 
GeneralRe: Found your next laptop Pin
David O'Neil29-Sep-21 12:12
professionalDavid O'Neil29-Sep-21 12:12 
GeneralRe: Found your next laptop Pin
Duke Carey30-Sep-21 0:26
professionalDuke Carey30-Sep-21 0:26 
GeneralRe: Found your next laptop Pin
Jon McKee29-Sep-21 12:29
professionalJon McKee29-Sep-21 12:29 
GeneralRe: Found your next laptop Pin
Richard Andrew x6429-Sep-21 13:31
professionalRichard Andrew x6429-Sep-21 13:31 
GeneralRe: Found your next laptop Pin
JohaViss611-Oct-21 1:09
professionalJohaViss611-Oct-21 1:09 
GeneralRe: Found your next laptop Pin
den2k8829-Sep-21 21:18
professionalden2k8829-Sep-21 21:18 
GeneralRe: Found your next laptop Pin
User 991608030-Sep-21 4:00
professionalUser 991608030-Sep-21 4:00 
GeneralRe: Found your next laptop Pin
dandy7230-Sep-21 5:28
dandy7230-Sep-21 5:28 
GeneralRe: Found your next laptop Pin
den2k8830-Sep-21 21:37
professionalden2k8830-Sep-21 21:37 
GeneralRe: Found your next laptop Pin
dandy721-Oct-21 3:06
dandy721-Oct-21 3:06 
GeneralRe: Found your next laptop Pin
Slow Eddie1-Oct-21 2:23
professionalSlow Eddie1-Oct-21 2: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.