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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:10:58+00:00 2026-05-10T21:10:58+00:00

If I define a struct in C# using automatic properties like this: public struct

  • 0

If I define a struct in C# using automatic properties like this:

public struct Address {     public Address(string line1, string line2, string city, string state, string zip)     {         Line1 = line1;         Line2 = line2;         City = city;         State = state;         Zip = zip;     }      public string Line1 { get; protected set; }     public string Line2 { get; protected set; }     public string City { get; protected set; }     public string State { get; protected set; }     public string Zip { get; protected set; } } 

When I attempt to build the file, I get a compilation error saying The 'this' object cannot be used before all of its fields are assigned to. This can be solved by changing the constructor to make a chained call to the default constructor like this:

public Address(string line1, string line2, string city, string state, string zip): this() {     Line1 = line1;     Line2 = line2;     City = city;     State = state;     Zip = zip; } 

My question is, why does this work, and what is happening? I have a guess, and I tried to prove it by looking at IL, but I’m only kidding myself if I think I can break down IL. But my guess is, auto properties work by having the compiler generate fields for your properties behind the scenes. Those fields cannot be accessed through code, all setting and getting must be done through the properties. When creating a struct, a default constructor cannot be explicitly defined. So behind the scenes, the compiler must be generating a default constructor that sets the values of the fields that the developer can’t see.

Any and all IL wizards are welcome to prove or disprove my theory.

  • 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-10T21:10:58+00:00Added an answer on May 10, 2026 at 9:10 pm

    Note: as of C# 6, this isn’t required – but you should be using read-only automatically-implemented properties with C# 6 anyway…

    this() makes sure that the fields are definitely assigned as far as the compiler is concerned – it sets all fields to their default values. You have to have a fully constructed struct before you can start accessing any properties.

    It’s annoying, but that’s the way it is. Are you sure you really want this to be a struct though? And why use a protected setter on a struct (which can’t be derived from)?

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

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • 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 A call to a static method will be compiled to… May 11, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer It Depends. Do I let exceptions bubble up? Absolutely. But… May 11, 2026 at 5:56 pm
  • Editorial Team
    Editorial Team added an answer There is no "intrinsic" order to a SQL table, so… May 11, 2026 at 5:56 pm

Related Questions

I'm working on a code base in which we have several configurable types. One
In .NET, a value type (C# struct ) can't have a constructor with no
I am trying to write a C# P/Invoke wrapper for a C API (a
The program is supposed to accept telnet connections on port 8888 and then send

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.