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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:34:22+00:00 2026-05-11T02:34:22+00:00

Kicking around some small structures while answering this post , I came across the

  • 0

Kicking around some small structures while answering this post, I came across the following unexpectedly:

The following structure, using an int field is perfectly legal:

struct MyStruct {      public MyStruct ( int size )      {          this.Size = size; // <-- Legal assignment.     }       public int Size;  } 

However, the following structure, using an automatic property does not compile:

struct MyStruct {      public MyStruct ( int size )      {          this.Size = size; // <-- Compile-Time Error!     }       public int Size{get; set;} } 

The error returned is ‘The ‘this’ object cannot be used before all of its fields are assigned to’. I know that this is standard procedure for a struct: the backing field for any property must be assigned directly (and not via the property’s set accessor) from within the struct’s constructor.

A solution is to use an explicit backing field:

struct MyStruct {      public MyStruct(int size)     {         _size = size;     }      private int _size;      public int Size     {         get { return _size; }         set { _size = value; }     } } 

(Note that VB.NET would not have this issue, because in VB.NET all fields are automatically initialized to 0/null/false when first created.)

This would seem to be an unfortunate limitation when using automatic properties with structs in C#. Thinking conceptually, I was wondering if this wouldn’t be a reasonable place for there to be an exception that allows the property set accessor to be called within a struct’s constructor, at least for an automatic property?

This is a minor issue, almost an edge-case, but I was wondering what others thought about this…

  • 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. 2026-05-11T02:34:23+00:00Added an answer on May 11, 2026 at 2:34 am

    From C# 6 onward: this is no longer a problem


    Becore C# 6, you need to call the default constructor for this to work:

    public MyStruct(int size) : this() {     Size = size; } 

    A bigger problem here is that you have a mutable struct. This is never a good idea. I would make it:

    public int Size { get; private set; } 

    Not technically immutable, but close enough.

    With recent versions of C#, you can improve on this:

    public int Size { get; } 

    This can now only be assigned in the constructor.

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

Sidebar

Ask A Question

Stats

  • Questions 141k
  • Answers 142k
  • 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 Let's turn this on its head: Why are you using… May 12, 2026 at 8:11 am
  • Editorial Team
    Editorial Team added an answer what about using the project phase or role to differentiate… May 12, 2026 at 8:11 am
  • Editorial Team
    Editorial Team added an answer The standard library does not currently support that. There is… May 12, 2026 at 8:11 am

Related Questions

I am kicking around the idea of using a workflow engine on this upcoming
First post time, I've been playing around with MVC abit... I have a view
Java is nearing version 7. It occurs to me that there must be plenty
Given a pretty basic source tree structure like the following: trunk ------- QA |--------

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.