1

I have found in many places that C# structures should be treated as immutable, but I would like somebody help me to understand the behavior of the following code:

public partial class Form1 : Form
{
    private struct SocSearchDomains
    {
        public string UrlName;
        public DateTime LastRequestStampDate;
        public int ErrorsCount;

        public SocSearchDomains(string urlName, DateTime requestStampDate)
        {
            UrlName = urlName;
            LastRequestStampDate = requestStampDate;
            ErrorsCount = 1;
        }
    }

    private static SocSearchDomains[] searchDomain { get; set; }
    private static SocSearchDomains searchDomain1 { get; set; } 

    public Form1()
    {
        InitializeComponent();
        searchDomain = new SocSearchDomains[1];
        searchDomain[0] = new SocSearchDomains("192.168.1.81", DateTime.Now);
        searchDomain1 = new SocSearchDomains("192.168.1.81", DateTime.Now);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(string.Format("SearchDomain[0]: {0}, SearchDomain1: {1}", searchDomain[0].ErrorsCount, searchDomain1.ErrorsCount));
        System.Threading.Interlocked.Increment(ref searchDomain[0].ErrorsCount);
        System.Threading.Interlocked.Increment(ref searchDomain1.ErrorsCount);
    }
}

Basically, when I try to increment SearchDomain1.ErrorsCount, I get a "cannot modify the return value... because it is not a variable", but increment works for searchDomain[0].ErrorsCount (when commenting Interlocked for SearchDomain1.ErrorsCount). Why is that behavior and is it safe to use such Interlocking (in an array of structures) in a multi-thread app?

Yury Kerbitskov
  • 643
  • 1
  • 7
  • 21
Gerardo H
  • 694
  • 6
  • 9
  • What is the reason SocSearchDomains is a struct and not a class? Or are you making a random artifical example? – Scott Chamberlain Sep 22 '16 at 15:16
  • look here, you the same situation. Your are storing value type, not reference type and it means that array returns you value, not a reference and you can't change it: http://stackoverflow.com/questions/1747654/cannot-modify-the-return-value-error-c-sharp – Yury Kerbitskov Sep 22 '16 at 15:24
  • @Scott, The reason is that all I need is to keep track of that data, no methods involved (just the constructor) and the only data that changes is the counter. So, and may be it is my misunderstanding, a structure in my case would be lighter (in memory) than using a class. – Gerardo H Sep 22 '16 at 15:26
  • [Classes and Structs (C# Programming Guide)](https://msdn.microsoft.com/en-us/library/ms173109.aspx) – Ňɏssa Pøngjǣrdenlarp Sep 22 '16 at 15:48

1 Answers1

0

Responding to why is that behavior, Choosing between Class and Struct help me to respond: "Value type arrays are allocated inline, meaning that the array elements are the actual instances of the value type". Now, for my second question if it is safe or not, I think it's better to follow the rule of when using a structure (in same link): If it is immutable. So, I will better use a class for that purpose.

Gerardo H
  • 694
  • 6
  • 9
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/13762240) – ADyson Sep 23 '16 at 09:30
  • I think that providing a reason, instead just stating "it does not provide an answer" and deleting my response would present a better opportunity to learn for everybody. – Gerardo H Sep 23 '16 at 23:42
  • the comment wording is automatically generated by Stackoverflow's Review system when I flagged the answer. You will see thousands of identical comments across the site if you spend long enough on it. FWIW, the convention on SO is to update your question if you find an answer to your own question. – ADyson Sep 24 '16 at 19:36