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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:26:50+00:00 2026-05-25T19:26:50+00:00

I have one observation about struct. When I declare a property in Struct and

  • 0

I have one observation about struct. When I declare a property in Struct and if I don’t initialize the Struct then it gives me the below error – “Use of unassigned local variable empStruct”

PSeduo Code-

struct EmpStruct
{
    private int firstNumber;
    public int FirstNumber
    {
        get { return firstNumber; }
        set { firstNumber = value; }
    }

    public int SecondNumber; 

}

Program.cs-

EmpStruct empStruct;
empStruct.FirstNumber = 5;

But when I declare public variable then the above code works.

EmpStruct empStruct;
empStruct.SecondNumber;

So my question is why compiler not gives error when i try to access variable.(In case of Class it will give the error).

  • 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-25T19:26:50+00:00Added an answer on May 25, 2026 at 7:26 pm

    There’s a tremendous amount of confusion in this thread.

    The principle is this: until all of the fields of an instance of a struct are definitely assigned, you can not invoke any properties or methods on the instance.

    This is why your first block of code will not compile. You are accessing a property without definitely assigning all of the fields.

    The second block of code compiles because it’s okay to access a field without all of the fields being definitely assigned.

    One way to definitely assign a struct is to say

    EmpStruct empStruct = new EmpStruct();
    

    This invokes the default parameterless constructor for EmpStruct which will definitely assign all of the fields.

    The relevant section of the specification is §5.3 on Definite Assignment. And from the example in §11.3.8

    No instance member function (including the set accessors for the properties X and Y) can be called until all fields of the struct being constructed have been definitely assigned.

    It would be more helpful (ahem, Eric Lippert!) if the compiler error message were along the lines of

    Use of not definitely assigned local variable empStruct.

    Then it becomes clear what to search for the in the specification or on Google.

    Now, note that you’ve defined a mutable struct. This is dangerous, and evil. You shouldn’t do it. Instead, add a public constructor that lets you definitely assign firstNumber and secondNumber, and remove the public setter from EmpStruct.FirstNumber.

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

Sidebar

Related Questions

I have one text input and one button (see below). How can I use
I don't have idea about overlay.I am stuck at this point.I am integration one
I have one user who gets an error message when he closes his browser.
I have the following matching problem: I have two data.frames, one with an observation
I have a JSF2/Richfaces 4 project in which I want to use one of
I have one field that I need to sum lets say named items However
I have one thread that writes results into a Queue. In another thread (GUI),
I have one hibernate sequence, that generates all sequence-numbers in my app. When I
I have one std::list<> container and these threads: One writer thread which adds elements
I have one website on my server, and my IIS Worker Process is using

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.