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
  • 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. 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

Related Questions

We have some code kicking around that uses this old internal Sun package for
I am kicking around the idea of using a workflow engine on this upcoming
All, I've had an idea kicking around for a while now: Is there a
This problem has been kicking my butt for a few days now. I have
I'm currently kicking off a background thread to do some REST queries in my
I'm sorry, but this is beginning to feel like kicking myself in the head.
The jqGrid has been kicking my butt (as well as others on this site).
I am kicking off some process improvements at a new company. One of the
I've done some homework on this and understand the basic rules of Version Control.
I am trying to get some kicking in python and decided to run python

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.