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 409819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:49:40+00:00 2026-05-12T17:49:40+00:00

Why is this example of code behaving differently in c++ and C# . [C++

  • 0

Why is this example of code behaving differently in c++ and C#.

[C++ Example]

int arr[2];
int index = 0;
arr[index] = ++index;

The result of which will be arr[1] = 1;

[C# Example]

int[] arr = new int[2];
int index = 0;
arr[index] = ++index;

The result of which will be arr[0] = 1;

I find this very strange. Surely there must be some rationale for both languages to implement it differently? I wonder what would C++/CLI output?

  • 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-05-12T17:49:40+00:00Added an answer on May 12, 2026 at 5:49 pm

    As others have noted, the behaviour of this code is undefined in C/C++. You can get any result whatsoever.

    The behaviour of your C# code is strictly defined by the C# standard.

    Surely there must be some rationale for both languages to implement it differently?

    Well, suppose you were designing C#, and wished to make the language easy for C++ programmers to learn. Would you choose to copy C++’s approach to this problem, namely, leave it undefined? Do you really want to make it easy for perfectly intelligent developers to accidentally write code that the compiler can just make up any meaning for that it wants?

    The designers of C# do not believe that undefined behaviour of simple expressions is a good thing, and therefore we have strictly defined what expressions like this mean. We cannot possibly agree with what every C++ compiler does because different C++ compilers give you different results for this sort of code, and so we cannot agree with all of them.

    As for why the designers of C++ believe that it is better to leave simple expressions like this to have undefined behaviour, well, you’ll have to ask one of them. I could certainly make some conjectures, but those would just be educated guesses.

    I’ve written a number of blog articles about this sort of issue; my most recent one was about almost exactly the code you mention here. Some articles you might want to read:

    How the design of C# encourages elimination of subtle bugs:

    http://blogs.msdn.com/ericlippert/archive/2007/08/14/c-and-the-pit-of-despair.aspx

    Exactly what is the relationship between precedence, associativity, and order of execution in C#?

    http://blogs.msdn.com/ericlippert/archive/2008/05/23/precedence-vs-associativity-vs-order.aspx

    In what order do the side effects of indexing, assignment and increment happen?

    http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to install the ruby-hmac module. See this blog… May 13, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer Looks to me like it should work - there's nothing… May 13, 2026 at 1:15 am
  • Editorial Team
    Editorial Team added an answer Seems your XSLT processor convert decimal numbers strings to float-point… May 13, 2026 at 1:15 am

Related Questions

I'm trying to create a simple example of an editable gridview, and for some
I try to implement point lights in OpenGL with GLSL. I send all the
In Scala it is possible formulate patterns based on the invididual characters of a
When a JAR of an application is created, the images in the application no

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.