Похожие чаты

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

36 просмотров

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

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

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

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта