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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:53:04+00:00 2026-05-23T16:53:04+00:00

I was just writing a property setter and had a brain-wave about why we

  • 0

I was just writing a property setter and had a brain-wave about why we don’t have to return the result of a set when a property might be involved in operator = chaining, i.e:

var a = (b.c = d);

(I’ve added the brackets for clarity – but it makes no difference in practise)

I started thinking – where does the C# compiler derive the value that is assigned to a in the above example?

Logic says that it should be from the result of the (b.c = d) operation but since that’s implemented with a void set_blah(value) method it can’t be.

So the only other options are:

  • Re-read b.c after the assignment and use that value
  • Re-use d

  • Edit (since answered and comments from Eric) – there’s a third option, which is what C# does: use the value written to b.c after any conversions have taken place

Now, to my mind, the correct reading of the above line of code is

set a to the result of setting b.c to d

I think that’s a reasonable reading of the code – so I thought I’d test whether that is indeed what happens with a slightly contrived test – but ask yourself if you think it should pass or fail:

public class TestClass
{
  private bool _invertedBoolean;
  public bool InvertedBoolean
  {
    get
    {
      return _invertedBoolean;
    }
    set
    {
      //don't ask me why you would with a boolean,
      //but consider rounding on currency values, or
      //properties which clone their input value instead
      //of taking the reference.
      _invertedBoolean = !value;
    }
  }
}

[TestMethod]
public void ExampleTest()
{
  var t = new TestClass();
  bool result;
  result = (t.InvertedBoolean = true);
  Assert.IsFalse(result);
}

This test fails.

Closer examination of the IL that is generated for the code shows that the true value is loaded on to the stack, cloned with a dup command and then both are popped off in two successive assignments.

This technique works perfectly for fields, but to me seems terribly naive for properties where each is actually a method call where the actual final property value is not guaranteed to be the input value.

Now I know many people hate nested assignments etc etc, but the fact is the language lets you do them and so they should work as expected.

Perhaps I’m being really thick but to me this suggests an incorrect implementation of this pattern by the compiler (.Net 4 btw). But then is my expectation/reading of the code incorrect?

  • 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-23T16:53:05+00:00Added an answer on May 23, 2026 at 4:53 pm

    The result of an assignment x = {expr} is defined as the value evaluated from {expr}.

    §14.14.1 Simple assignment (ECMA334 v4)

    …
    The result of a simple assignment expression is the value assigned to
    the left operand. The result has the same type as the left operand,
    and is always classified as a value.
    …

    And note that the value assigned is the value already evaluated from d. Hence the implementation here is:

    var tmp = (TypeOfC)d;
    b.c = tmp;
    a = tmp;
    

    although I would also expect with optimisations enabled it will use the dup instruction rather than a local variable.

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

Sidebar

Related Questions

Just writing some CSS and I have a pretty cool Search form where you
I am just about done writing my first mergesort program and am running into
I have a very complex Linq to SQL query that returns a result set
I have a very strange problem and i don't know what to do about
I'm about to update my code to use addEventListener() over just writing to the
I'm just about to start writing a new application and I wanted to sort
I spent a few days writing a test, then had to add a property
I am writing a custom control, and I have a property path as string
I'm writing a data access layer where I want to return just the data
I was just writing a procedure that is looking for a newline and I

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.