1

in the below code who is assigning the value for the variable a? why a default constructor is created when I did not write anything about it in the code (I checked the ildasm for it where an default constructor is created )

contents in the created default constructor:

method public hidebysig specialname rtspecialname 
    instance void  .ctor() cil managed
{
// Code size       8 (0x8)
.maxstack  8
IL_0000:  ldarg.0
IL_0001:  call       instance void [System.Runtime]System.Object::.ctor()
IL_0006:  nop
IL_0007:  ret
} // end of method Program::.ctor

Code:

using System;
namespace demo
{
    class Program
    {
        int a;
        static void Main(string[] args)
        {
            Program obj = new Program();
            Console.WriteLine(obj.a);
            Console.ReadLine();
        }
    }
}

in the above code the output is 0, who assigned the value for it? either default constructor created or CLR?

instance void [System.Runtime]System.Object::.ctor() What does this mean in the ildasm code of the default constructor

  • 1
    try to initialize a in the field initializer, like `int a = 8;` see what is the difference. – kennyzx Jan 16 '19 at 10:24
  • @kennyzx i tried the same `int a=8` then the created default constructor has assigned the value 8 to the a –  Jan 16 '19 at 10:28
  • 2
    **Without a constructor, you can't create an object**. That is why a default constructor was added for you. Since the compiler effectively has two options if you forget to provide a constructor. a) Provide one for you. b) Not compile (and force you to add one manually to compile). C# chose option a). – mjwills Jan 16 '19 at 10:33
  • so in ildasm contains a call of `System.Object::.ctor()` is a constructor of the `system.Object` class which is used for object creation. @mjwills –  Jan 16 '19 at 10:37

1 Answers1

1

Basics. All in the documentation - and quite early.

in the above code the output is 0, who assigned the value for it?

Variables in .NET are all initialized to 0 to avoid reusing memory fragments from earlier use of the memory address.

why a default constructor is created when I did not write anything about it in the code

Because it is a DEFAULT constructor that is created BECAUSE (!) you do not specify a constructor. Maybe there is a confusion about the word "default"? It makes it easier if all objects have constructors, particularly if I subclass your object and then call it ;)

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 1
    `It makes it easier if all objects have constructors` Easier is an odd term to use here - since a (non static) class without **any** constructor wouldn't be hard, it would be useless. :) – mjwills Jan 16 '19 at 10:26
  • 1
    Except you CAN subclass Program - it is not a static class - and the subclass calls the parent constructor as default. And then you can add code there and not create an issue. – TomTom Jan 16 '19 at 10:30
  • You can do that, yes, but only if the base class had a constructor (i.e. it didn't make it easier, it made it **possible**). I am not disagreeing with you - just saying that using the term `easier` is odd. – mjwills Jan 16 '19 at 10:32
  • thanks for the clarification @TomTom –  Jan 16 '19 at 10:40