check for item not being null,
Same as:
Action action;
Void execute() => action?.Invoke();
How is this different from :
Void execute() => action!.Invoke();
using exclamation mark instead?
It tells the compiler that this is definitely not null, so any null checking can be avoided.
You’re basically saying “trust me, I know what I’m doing” to the compiler, but realistically, you’re probably actually saying “hold my beer”.
😂😂😂i love this explanation
Thanks, so this means that using '!' should raise an error if item is null, unlike '?' which don't execute if null, right? Or there won't be any error in both cases?
my understanding is that "!" is the point where - before that the value could be null - after that, the compiler can consider that the expression (value!) is not null (in a #nullable enabled code) - at the time of ! if the value is null, it raise a NullRefException
it's not just that. it's really an NRT-warning forgiving operator: List<string?> a = new(); List<string> b = a.ToList()!;
so what happens if 'a' contained null elements, and you access them in b?
the same way it worked before C# 8.0
so basically it will throw nullref ? then what's the point of #nullable if the compiler has to test for null anyway? 🤔
it will throw null ref here: string? a = null; int b = a!.Length; but not here: string? a = null; string b = a!;
it makes sense, since string is nullable value by default. So according to this, then this should also raises an issue: int? a = null; int b = a!;
this will throw exception
Обсуждают сегодня