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).
The only way you could determine if a struct was initialized would be to check each element within it to see if it matched what you considered an initialized value for that element should be.
Another method to initialize struct members is to declare a variable and then assign each member with its corresponding value separately. Note that char arrays can't be assigned with string, so they need to be copied explicitly with additional functions like memcpy or memmove (see manual).
We can't initialize because when we declared any structure than actually what we do, just inform compiler about their presence i.e no memory allocated for that and if we initialize member with no memory for that.
Unlike classes, structs can be instantiated without using the New operator. If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.
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
andY
) 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
.
Regarding fields C# language Specification says:
10.5.4 Field initialization
The initial value of a field, whether it be a static field or an instance field, is the default value (§5.2) of the field’s type. It is not possible to observe the value of a field before this default initialization has occurred, and a field is thus never “uninitialized
11.3.4 Default values
However, since structs are value types that cannot be null, the default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null. The default value of a struct corresponds to the value returned by the default constructor of the struct (§4.1.2).
PS: in case of class it gives error because reference type value by default is null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With