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

  • Home
  • SEARCH
  • 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 732901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:11:42+00:00 2026-05-14T07:11:42+00:00

From http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx using System; struct MutableStruct { public int Value { get; set; }

  • 0

From http://csharpindepth.com/Articles/Chapter8/PropertiesMatter.aspx

using System;

struct MutableStruct
{
    public int Value { get; set; }

    public void SetValue(int newValue)
    {
        Value = newValue;
    }
}

class MutableStructHolder
{
    public MutableStruct Field;
    public MutableStruct Property { get; set; }
}

class Test
{    
    static void Main(string[] args)
    {
        MutableStructHolder holder = new MutableStructHolder();
        // Affects the value of holder.Field
        holder.Field.SetValue(10);
        // Retrieves holder.Property as a copy and changes the copy
        holder.Property.SetValue(10);

        Console.WriteLine(holder.Field.Value);
        Console.WriteLine(holder.Property.Value);
    }
} 

1) Why is a copy (of Property?) being made?
2) When changing the code to holder.Field.Value and holder.Property.Value = 10, I get the error below. That just blew my mind.

Cannot modify the return value of ‘MutableStructHolder.Property’ because it is not a variable

Why would I ever not be allowed to assign a value inside of a property!?! Both properties are get/set!

And finally WHY would you EVER want behavior mentioned in 1 and 2? (It never came up for me, I always used get only properties).

Please explain well, I can’t imagine ever wanting the 2nd much less then the first. It is just so weird to me.

  • 1 1 Answer
  • 1 View
  • 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-14T07:11:42+00:00Added an answer on May 14, 2026 at 7:11 am

    To answer #1: Consider how it would look without an auto-generated property. You’d have Property { get { return xxx; } } That’s where the copy is made. What looks like referring to a property is really calling a getter method. Would you expect an in-place edit if you had:

    private MutableStruct v;
    public MutableStruct f()
    {
      return v;
    }
    
    f() = new MutableStruct();
    

    Of course not, we all depend on return to make a copy of the struct so that the private variable is protected against outside modification. So that’s why it works the way it does.

    Now, if you’re thinking that the property setter should have automatically been called with the resulting value, please let me point out that it’s extremely difficult impossible to know at compile-time if a call to one of the struct’s methods makes a change (consider if the struct is defined in a different assembly). And writing the value back isn’t just inefficient-but-harmless, it might break correctness. Consider a multithreading scenario, where threads that were only reading from the property under protection of a ReaderWriterLock suddenly are writing back to it. All sorts of pain and misery would occur. So the C# compiler automatically setting the property to the result of your computation just isn’t an option.

    EDIT: You were wondering why setting a field on the struct generates an error message, but calling a method to do the same thing doesn’t. Well, the method might have useful side effects, so you still need to be able to call it on a temporary copy of the struct. Consider the following variation:

    struct Mutable
    {
        public static int Sum = 0;
    
        public int x;
        public Mutable(int x) { this.x = x; }
        public void Total() { Sum += x; }
    }
    
    class Container
    {
        public Mutable Field;
        public Mutable Property { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Container c = new Container();
            c.Field = new Mutable(1);
            c.Property = new Mutable(2);
            c.Field.Total();
            c.Property.Total();
            Console.Out.WriteLine(Mutable.Sum);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the .NET TWAIN code from http://www.codeproject.com/KB/dotnet/twaindotnet.aspx?msg=1007385#xx1007385xx in my application. When I try
From http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(VS.71).aspx: [C++] [StructLayout(LayoutKind::Explicit, Size=16, CharSet=CharSet::Ansi)] __value class MySystemTime { public: [FieldOffset(0)] short int
From http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx it looks like, that this type of sockets are using more than
From http://msdn.microsoft.com/en-us/library/aa287786(v=vs.71).aspx public string Name { get { return name; } set { name
From http://blogs.msdn.com/b/michael_howard/archive/2007/04/04/codegear-s-new-delphi-2007-supports-aslr-and-nx.aspx , I am using {$SETPEOPTFLAGS $140} in my project file right under
From : http://technet.microsoft.com/en-us/library/cc288905.aspx?ppud=4 Do not use /* to indicate wildcard managed sites at the
From http://java.sun.com/developer/technicalArticles/J2SE/security/#3 : Note: These two system properties are ignored when applications run on
Quote from: http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx "Invoking an event can only be done from within the class
Download JQGrid js file from http://www.trirand.com/blog/ . Is it free? What is this http://www.trirand.net/demoaspnetmvc.aspx
Using the Generator class from http://unpythonic.blogspot.com/2007/08/using-threads-in-pygtk.html , the following code makes my program fail:

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.