Live data from Hacker News

What's New in C# 14: Null-Conditional Assignments

blog.ivankahl.com

1–10 of 174 posts

Re: What's New in C# 14: Null-Conditional Assignments

#3
post #2

Isn't this more confusing? Because it skip the code if the value is null and I don't think it is normal to follow the flow assuming nothing has happened.

It’s for the use case where they’d skip it anyway so that would be intended behavior.

Re: What's New in C# 14: Null-Conditional Assignments

#4
Looks interesting & I'm excited to try this out myself. I like the more verbose null/error handling personally in professional code, but maybe that's because im still working in framework! I'll certainly be using these in my personal projects that'll be on .NET 10

Re: What's New in C# 14: Null-Conditional Assignments

#6
post #2

Isn't this more confusing? Because it skip the code if the value is null and I don't think it is normal to follow the flow assuming nothing has happened.

> I don't think it is normal to follow the flow assuming nothing has happened.

I think it is for situations where the programmer wants to check a child property but the parent object may be null. If the parent is expected to be null sometimes, the syntax lets the programmer express "try to get this value, but if we can't then move on" without the boilerplate of explicitly checking null (which may be a better pattern in some cases).

It's sort of like saying:

- Get the value if you can, else move on. We know it might not be there and it's not a big deal.

v.s.

- The value may not be there, explicitly check and handle it because it should not be null.

Re: What's New in C# 14: Null-Conditional Assignments

#8
post #2

Isn't this more confusing? Because it skip the code if the value is null and I don't think it is normal to follow the flow assuming nothing has happened.

That's already the case for the null coalescing operator when it ends in a method call: the method call is skipped if the base is null. For instance, we can invoke event handlers with "myEvent?.Invoke(...);" and the call will be skipped if there are no event handlers registered, and this is the canonical way to do it.

Re: What's New in C# 14: Null-Conditional Assignments

#10
post #2

Isn't this more confusing? Because it skip the code if the value is null and I don't think it is normal to follow the flow assuming nothing has happened.

From the article:

> If config?.Settings is null, the assignment is skipped.

If the right hand expression has side effects, are they run? I guess they do, and that would make the code more predictable.

Post reply on HN