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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:45:21+00:00 2026-05-23T06:45:21+00:00

The following code is illegal: public struct MyStruct { public MyStruct(int a, int b)

  • 0

The following code is illegal:

public struct MyStruct
{
    public MyStruct(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    public int a;
    public int b;
}

//now I want to cache for whatever reason the default value of MyStruct
MyStruct defaultValue;
...
if (foo != defaultValue) //use of unassigned variable...

The way things should be done is obviously:

MyStruct defaultValue = default(MyStruct) //or new MyStruct();
...
if (foo != defaultValue) //compiler is happy

But the following is also allowed (I didn’t know this and stumbled upon it by accident):

MyStruct defaultValue;
defaultValue.a = 0;
defaultValue.b = 0;
...
if (foo != defaultValue) //legal

I guess the compiler verifies that all fields of the struct have been initialized and therefore allows this code to compile. Still I find it confusing with how the rest of the language works. After all, you are basically using an unassigned variable in the C# way of seeing things.

Things get even more confusing if you consider the following code:

public struct MyStruct
{
    public MyStruct(int a, int b)
    {
        this.a = a;
        this.b = b;
        this.c = (a + b).ToString();
    }

    public int a;
    public int b;
    internal string c;
}

The following code is illegal becuase in this case we haven’t assigned all visible fields:

MyStruct defaultValue;
defaultValue.a = 0;
defaultValue.b = 0;
...
if (foo != defaultValue) //use of unassigned variable...

Visible is the key word here, because if MyStruct were to be defined in a referenced assembly then c is not visible and the compiler will not complain and the previous code would be perfectly valid. Confusing again.

Can somebdoy please explain why its allowed to initialize a struct in C# in such manner? Why not disallow it completely so there is a more unified experience when dealing with any type of value in the language?

EDIT 1: I made a mistake in the last example. The compiler will be happy only if the not visible field is of reference type. Even more confusing. Is this last case a known bug in the compiler or is there a sane reason for it to work the way it does?

Changed last example to a valid case.

EDIT 2: I’m still a litte befuddled with how value-type initialization works. Why isn’t the following allowed for instance:

struct MyStruct
{ 
    public int A { get; set; } //Auto-implemented property
    public int B { get; set; } //Auto-implemented property
}

MyStruct defaultValue;
defaultValue.A = 0;  //use of unassigned variable...
defaultValue.B = 0;

The way I see it, there is little doubt that all fields MyStruct are initialized. I can see the reasoning of why this wouldn’t be allowed if properties were not auto-implemented as it is arguably possible that the setters do not garantee that all fields are set. But in auto-implemented properties the compiler knows with 100% certainty that the fields will be set if the properties are (after all its code that the compiler generates for you).

Finally, a small theoretical case with evidently no practical use:

struct MyUselessStruct
{
}

MyUselessStruct defaultValue;
Console.WriteLine(defaultValue.ToString()); //hehe did I get away with using an unassigned variable?

Then why isn’t this allowed:

Object object;
if (object == null) .... //use of unassigned variable

I find both cases similar in concept and I’d expect them both to work the same way in C#. I still don’t understand why this seemingly useless differentiation in the way value-type variables can be initialized and what is it’s practical use (on top of the inconsistencies I explained in the first part of my question)

  • 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-23T06:45:22+00:00Added an answer on May 23, 2026 at 6:45 am

    The spec explicitly allows this; 12.3 in ECMA334v4

    • A struct-type variable is considered definitely assigned if each of its instance variables is considered definitely assigned.

    However, mutable structs are evil. So I strongly suggest you DO NOT do this.

    Make the fields private readonly, set them via a custom constructor, and access them via a get-only property:

    public struct MyStruct
    {
        public MyStruct(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
        public int A { get { return a; } }
        public int B { get { return b; } }
        private readonly int a, b;
        internal int C { get { return a + b; } }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
The following code: template <typename S, typename T> struct foo { void bar(); };
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code should find the appropriate project tag and remove it from the
The following code has a simple binding which binds the Text of the TextBlock
The following code executes a simple insert command. If it is called 2,000 times
The following code number=1 if [[ $number =~ [0-9] ]] then echo matched fi

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.