! has shocking behavior #5968
-
EDIT: Found this awesome project https://github.com/tom-englert/Nullable.Extended I wrote this line of code today in .net 6
And then started up my app to verify the error that would be thrown since this env var doesn't exist yet, and CPStorage() requires a non-null string. (And I was as expected getting a nullability warning before I added the !) And to my dismay, it started up with no error. I was shocked. I have used swift, where ! will trap at runtime if it is null. I expected c# not to trap (Exit), but throw a null exception. If I wanted to turn off a compiler warning and do something unsafe, I would have used a I have been slogging through asp.net for a new project, and have been willing to overlook a lot of struggles because I think C# is a decent language, but this has me worried I'm making a bad bet. |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 28 replies
-
Tagging subscribers to this area: @dotnet/area-infrastructure-libraries Issue DetailsI wrote a line of code today,
|
Beta Was this translation helpful? Give feedback.
-
I seem to have hit ctrl-enter or shfit-enter very early when typing my code (annoying feature to be honest), so it prematurely called the bots. |
Beta Was this translation helpful? Give feedback.
-
If it's used in the middle of a member invocation expression (e.g. For the line of code as written I'd expect the In C# the suffix
|
Beta Was this translation helpful? Give feedback.
-
@kjpgit what tools are you using? If you use Visual Studio I suggest you hit debug to see what's happening. |
Beta Was this translation helpful? Give feedback.
-
So let's see how to write safe, modern c# code in 2022:
I vote for a The SHORTEST code had better be the correct thing, because that's what everyone will do. I'm guessing people are just using ! everywhere because the compiler isn't don't good enough control flow analysis, but there should be a different, longer symbol (or system function) for that and it should be marked as I can not express how disappointed I am with this "new philosophy" of the c# stewards, where unsafe code is now the easiest and shortest path. Apple's Swift language got this right with the ! unwrap operator (although I'd prefer an exception instead of terminating the thread, for server software obviously). Please transfer this issue to the language repo if appropriate. Thank you. |
Beta Was this translation helpful? Give feedback.
-
@kjpgit nobody here is saying you should write that code. We don't yet understand what the problem is. |
Beta Was this translation helpful? Give feedback.
-
Starting at the start again. If this environment variable does not exist, GetEnvironmentVariable will return null as you know. What happens now depends on the implementation of CPStorage:
Does that match your expectations? In no circumstances do you need to write the extended code you have pasted. |
Beta Was this translation helpful? Give feedback.
-
You can use
The nullable reference type feature of C# 8 is designed to be pure compile time check, to avoid breaking existing code as possible. |
Beta Was this translation helpful? Give feedback.
-
Also, there's supposedly will be |
Beta Was this translation helpful? Give feedback.
-
Why not just write an extension method? public static class _Object_Extensions
{
[MethodImpl(AggressiveInlining)]
public static T NotNull<T>(this T? This) where T : class => This ?? throw new NullReferenceException();
}
string? s = null;
Foo(s.NotNull()); |
Beta Was this translation helpful? Give feedback.
You can use
?? throw
expression, or you can make the__unwrap
extension method.!
is designed for when nullability is obvious to human, but not provable for the compiler. It's intentionally not checking for performance reason, for example the usage inImmutableArray<T>
.The nullable reference type feature of C# 8 is designed to be pure compile time check, to avoid breaking existing code as possible.