Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I do ??= in C#?

I often find myself doing:

foo = foo ?? x;

Why can't I do:

foo ??= x;

Edit: I know it's not part of the language... My question is "why not"? I find the necessity to repeat "foo" to be unpleasing and potentially error-prone. It looks just as ugly as:

foo = foo + x;
like image 461
JoelFan Avatar asked Feb 17 '09 18:02

JoelFan


People also ask

Why is C difficult?

It is hard to learn because: It has complex syntax to support versatility. It is a permissive language—you can do everything that's technically possible, even if not logically right. It is best learned by someone who already has a foundation with C programming.

Is C language hard for beginners?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.

Can you do everything in C?

You can do almost everything in any of the programming languages. Of course the way of expressing it will vary, as well as the amount of code, clarity of code, ease of further maintenance. Some tasks can be coded with few lines in Prolog and few pages of code in C++, and so on.

Is C good for beginners?

Hence, it is the best language for those who are new to programming. C programming language uses blocks to separate pieces of code performing different tasks. This helps make programming easier and keeps the code clean. Thus, the code is easy to understand even for those who are starting out.


2 Answers

When I think about it,

foo = foo ?? x 

is really just

foo = foo != null ? foo : x

and at that point, the analogy to += starts to fall apart.

like image 52
Brian Genisio Avatar answered Oct 22 '22 11:10

Brian Genisio


There's no reason why there couldn't be such an operator, but I suspect the added complexity of the language, though slight, outweights the benefit (which I would deem "very slight").

Basically any addition to the language has a pretty high bar to clear. You say you use this quite often - how often, really? While I deem ?? handy, I can't say I use even that terribly frequently.

like image 36
Jon Skeet Avatar answered Oct 22 '22 12:10

Jon Skeet