Похожие чаты

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 ответов

12 просмотров

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

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

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

А как старый хаскел с новым стыковать ? потому как тут работает https://play.haskell.org/saved/C3xpMzcd, а вот тут https://stepik.org/lesson/7602/step/9?unit=1473 нет ошибка C...
Fedor
131
Вопрос я правильно понимаю что в коде newtype ArrowMap k v = ArrowMap { getArrowMap :: k -> Maybe v } getArrowMap есть функция типа k -> Maybe v, если да, то не понимаю задач...
Fedor
64
I am honored to report that all the deliverables for the BCHFAQ Flipstarter Phase 1 have been completed. I appreciate the opportunity I've had to put all this together using t...
fixthetracking
15
Как Вы считаете нормально ли в двадцатых годах 21 века в ВУЗах Российской Федерации обучать студентов работе с TASM? Не слишком ли это "архаично"? (Если оффтоп или флейм для э...
Spiker01
52
Is there any new news from Kadena? The price is very suppressed.
HAKAN TECİMEN
27
Продолжая диалог про свифт в проде – сейчас возник вопрос в активном наборе бекендеров. В основном в нашей компании мы фанаты Java Spring и полностью ей довольны. Однако найм ...
Guseyn
27
{!! $modx->runSnippet( 'multiTV',[ 'tvName'=>'slider-main1', 'docid'=>'[*id*]', 'tplConfig'=>'', 'display'=>'4', 'offset'=>'0', 'rows'=>'all', 'ra...
Роман С
4
Комрады, хотел уточнить. Проперть в OnDestroy юнита-хозяина по-прежнему доступна? И еще уточнение: finalization юнита наступает раньше или позже OnDestroy?
Ed Doc
48
Hello everyone! I have CHZ coins in the BEP2 network, what should I do to change the network, since Binance stops entering coins in this network???
Ts. Ts.
22
Читаю сейчас [нет, уже больше не читаю!] курсовую о Булгакове, написанную, похоже, с помощью ChatGPT. Это удивительный психоделический опыт. Текст в основном написан в стиле б...
✨ Uni [🌊 В отпуске]
1
Карта сайта