Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:53+00:00 2026-06-12T09:14:53+00:00

In C#, if I write int? x = null; x += x ?? 1

  • 0

In C#, if I write

int? x = null;
x += x ?? 1

I would expect this to be equivalent to:

int? x = null;
x = x + x ?? 1

And thus in the first example, x would contain 1 as in the second example. But it doesn’t, it contains null. The += operator doesn’t seem to work on nullable types when they haven’t been assigned. Why should this be the case?

Edit: As pointed out, it’s because null + 1 = null and operator precedence. In my defence, I think this line in the MSDN is ambiguous!:

The predefined unary and binary operators and any user-defined
operators that exist for value types may also be used by nullable
types. These operators produce a null value if [either of] the
operands are null; otherwise, the operator uses the contained value to
calculate the result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:14:53+00:00Added an answer on June 12, 2026 at 9:14 am

    Here is the difference between the two statements:

    x += x ?? 1
    x = (x + x) ?? 1
    

    The second isn’t what you were expecting.

    Here’s a breakdown of them both:

    x += x ?? 1
    x += null ?? 1
    x += 1
    x = x + 1
    x = null + 1
    x = null
    
    x = x + x ?? 1
    x = null + null ?? 1
    x = null ?? 1
    x = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to write a function like this: int get_some_int(string index) { ...perform
At the moment I write stored procedures this way: create proc doStuff @amount int
This is my table definition: CREATE TABLE orders_total ( orders_total_id int unsigned NOT NULL
Consider a table such as this: CREATE TABLE records ( id INT NOT NULL
Given a table of structure: CREATE TABLE [dbo].[Example]( [FiscalYear] INT NOT NULL, [Hours] [decimal](28,
This is my database SQL code; CREATE TABLE ProductType ( ptID int not null
How would you write this code? This particular question is about a maze game
Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,
If I write int *columns[32]; am I defining an array with 32 pointers to
If in C I write: int num; Before I assign anything to num ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.