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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:00:35+00:00 2026-05-30T07:00:35+00:00

Considering the following struct : struct S { public string s; } What is

  • 0

Considering the following struct :

    struct S
    {
        public string s;
    }

What is the difference between 1 :

    S instance = new S();
    instance.s = "foo";

and 2 :

    S instance;
    instance.s = "foo";

Both versions compile and run fine.
I am just curious to know what happens behind the scenes.

Edit :
I guess in case 2 S is unassigned until we put a value on its s field;
As this doesn’t work :

 S instance;
 if (inst.s == null)
     inst.s = "foo";  //Compiler drops : Use of possibly unassigned field 's'

while this does :

 S instance;
 inst.s = "foo";
 if (inst.s == null)
     inst.s = "bar";  //Compiler drops : Use of possibly unassigned field 's'

and this also works :

 S inst = new S();     
 if (inst.s == null)
      inst.s = "foo";

I welcome any deeper explanations about this behavior

Update

I found those 2 posts, completing Marc’s answer :
why are mutable structs evil
when to use struct in c#

  • 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. Editorial Team
    Editorial Team
    2026-05-30T07:00:37+00:00Added an answer on May 30, 2026 at 7:00 am

    What is the difference between

    S instance = new S();     
    instance.s = "foo"; 
    

    and

    S instance;     
    instance.s = "foo";
    

    ?

    As Marc correctly points out, both are equally bad; the right thing to do is to make an immutable struct that takes the string in its constructor.

    And as Marc correctly points out, functionally there is no difference.

    However, that does not answer the question that you actually asked, which is "what happens behind the scenes?" By "behind the scenes" I’m assuming that you’re talking about the compiler’s semantic analysis of the code, as described in the C# specification.

    Fortunately the specification is extremely clear on the difference between these two cases.

    First off, as you correctly note, in the first case the variable is considered to be definitely assigned after the first statement. In the second case the variable is not considered to be definitely assigned until after the second statement.

    However, the definite assignment analysis is simply a consequence of the actual meaning of the code, which is as follows.

    The first fragment:

    • allocates storage for instance
    • allocates temporary storage for the temporary value
    • initializes the temporary value to the default struct state
    • copies the bits of the temporary value to the storage for instance
    • now instance is definitely assigned because all its bits were copied from another value
    • copies the string reference into the storage for instance

    The second fragment

    • allocates storage for instance
    • copies the string reference into the storage for instance
    • now instance is definitely assigned because all its fields have been assigned

    The compiler is permitted to notice that there is no way to determine whether or not a temporary was created, initialized and copied. If it does determine that then it is permitted to elide the creation of the temporary, and generate the same code for both fragments. The compiler is not required to do so; this is an optimization and optimizations are never required.

    Now, you might wonder under what circumstances you can determine that a temporary was created, initialized and copied. If you do wonder that then read my article on the subject:

    https://ericlippert.com/2010/10/11/debunking-another-myth-about-value-types/

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

Sidebar

Related Questions

Considering following code public class A { public static void main(String[] args) { new
Considering the following case in C#: class Foo { public Foo() { } }
Considering the following Scala snippet: case class Foo(v1: String, v2: Int, v3: Any) def
Considering the following: public class Person { public Person(string fName, string lName) { this.firstName
considering the following enum: public enum LeadStatus { Cold = 1, Warm = 2,
Considering the following as two versions of a very simple function in Python ,
Considering the following c code: typedef struct ELE *tree_ptr struct ELE { long val;
Considering following code: class Results { public int playerId; public int score; public int
What are the differences between YAML and JSON, specifically considering the following things? Performance
Considering the following code public bool GetFalse() { return false; } public bool GetTrue()

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.