Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C#, instance fields in a struct type (can't be initialized at declaration) are either always initialized by the the default parameter-less constructor with the respective default value or by any parameterized constructor explicitly defined by the programmer.

public struct MyStruct
{
    private string str1;

    private int num1;

    //parametized constructors in a struct always obligated to initialize instance fields
    public MyStruct(string strParameter, int numParameter)
    {
        str1 = stringParameter;

        num1 = numParameter;
    }
}


With instance fields in a class type however, fields are either intialized with a default value by the parameter-less constructor, but parameterized constructor are not obligated to initalize the fields as with struct types:

public class MyClass
{
    private string str1;

    private int num1;

    //parametized constructors in a class not obligated to initialize instance fields
    public MyClass(string strParameter, int numParameter)
    {
        //nothing in the body
    }
}


If a paramterized constructor of a class is not obligated to initialize the instance fields, then what initializes them with their default value, when the parameterized constructor is called?

What I have tried:

Was unable to resolve this problem after investegation.
Posted
Updated 9-Jun-21 22:25pm
Comments
Richard MacCutchan 10-Jun-21 4:17am    
The initial values are set in the compiled object. The string is null and the number is 0. Stepping through the code with the debugger shows this.

The CLR automatically zeros the memory it allocates for variables. This will set all types to their default value - 0 for numbers, false for booleans, null for reference types, etc.

For classes and structs, this will set all fields to their default values.

The C# compiler will generate a CS0165 error if you try to read a local variable without setting it to a value first. But that's only because this is usually an indication of an error with your code. The variable will technically have been initialized to its default value by the CLR.

The compiler will not generate a similar error for an uninitialized field - especially since structs have no good way to initialize their fields to anything other than the default. They can't have parameterless constructors, and they can't have field initializers. IIRC, this is to speed up allocation of arrays of structs - the CLR can just zero out the whole memory, rather than having to run code on each item in the array.
 
Share this answer
 
Comments
[no name] 11-Jun-21 0:25am    
In that case the C# compiler is responsible for intializing instance fields or static fields that were not initialized at declaration of by any parameterized costructor. Wheteher in a struct type or class type?
Richard Deeming 11-Jun-21 4:07am    
No, it's the CLR[^] which zeros out the memory, not the compiler.
I don`t exactly understand your question ... but basicly you as the developer are responsible for initializing a variable - that could be done when you declare it :
C#
private string str1 = "default-Value";
private int num1 = 123;
 
Share this answer
 
Comments
[no name] 10-Jun-21 2:13am    
instance fields of in a struct type cannot be instialized in the same line of code in which they are declared. Thus, are either initialized via the invokation of the parameter-less constructor which intializes them with their appropriate default value (as stated in this link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors#parameterless-constructors) or intialized via the invocation of a parametized which are intialized via the use of the input parameters. Parameterized constructor of a struct type are obligated to do so.

However, in the case of instance fields in a class type, they can be instialized in the same line of code in which they are declared. But my question about them not intialized in the same line of code in which they are defined in. Thus, are Thus, are either initialized via the invokation of the parameter-less constructor which intializes them with their appropriate default value or intialized via the invocation of a parametized which are intialized via the use of the input parameters. However, because Parameterized constructor of a class type are NOT obligated to do so. Then what is responsible for intializing the fields with their defualt value in the case that the instance fields are NOT intialized with a custom value when the parameterized constructor executes.

Is the compiler responsible for doing that? I would assume the parameter-less constructor no longer has a hand in assigning default values since it is was not invoked.
Ralf Meier 10-Jun-21 4:22am    
I'm sorry - I refered to your Class-Example ...
The Struct must be initialized when used as a Variable (perhaps with the Constructor of the Class which uses it)
[no name] 11-Jun-21 0:25am    
In that case the C# compiler is responsible for intializing instance fields or static fields that were not initialized at declaration of by any parameterized costructor. Wheteher in a struct type or class type?
Ralf Meier 11-Jun-21 1:28am    
As Richard wrote in the other Solution : if you want to have a defined content you are responsible by yourself to ensure that.
I would never let a Variable unassigned - but that's my 2 cent ...
[no name] 11-Jun-21 1:39am    
I agree with you but I am just wondering (and thus, what the question is all about) what is responsible for assigning a defualt value to unassigned instance fields (of a class type or struct type)???

C# specs says for static fields the C# compiler initializes them if they are unassigned at declaration and not initialized by a a static contructor.

However, for instance fields, all it says is the defualt constructor or parameterized constructors are responsible for doing so. But under the hood is the C# compiler still have a part in initalizing uninitailized instance fields of a class type or struct type?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900