Похожие чаты

Hi, I know that the question mark can be used to

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?

12 ответов

29 просмотров

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”.

Adnan-Al_Beda Автор вопроса
Shafiq Jetha
You’re basically saying “trust me, I know what I’m...

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?

Adnan Al_Beda
Thanks, so this means that using '!' should raise...

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

Wizou
my understanding is that "!" is the point where - ...

it's not just that. it's really an NRT-warning forgiving operator: List<string?> a = new(); List<string> b = a.ToList()!;

Ronny Gunawan ️
it's not just that. it's really an NRT-warning for...

so what happens if 'a' contained null elements, and you access them in b?

Ronny Gunawan ️
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? 🤔

Wizou
so basically it will throw nullref ? then what's t...

it will throw null ref here: string? a = null; int b = a!.Length; but not here: string? a = null; string b = a!;

Adnan-Al_Beda Автор вопроса
Ronny Gunawan ️
it will throw null ref here: string? a = null; in...

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!;

Похожие вопросы

Обсуждают сегодня

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
9
Карта сайта