tags:

views:

103

answers:

6

Hi everyone!In a class there will be a constructor.If a programmer defines that then definitely it will have a body. But if we don't define it then will that constructor will have a default body in it?

A: 

If you leave the implementation of the default constructor to the compiler (which is what happens if you don't define it) then the constructor will initialize all fields to their default values E.g.

class With<T>
{
  T field;
  string str;
  With()
  {
    field = default(T);
    str = "";
  }
}

class WithOut<T>
{
  T field;
  string str = "";
}

will from a functional point of view have the same default constructor

Rune FS
A: 

If you don't define a constructor, there will be a default empty constructor that will be considered internally by the compiler. It can be used to create new instances for that class, not to assign class properties/ members.

Rashmi Pandit
A: 

The default constructor will not do anything, it is just there to fulfil the requirement that a class has a constructor.

Dr Herbie
it's incorrect that it will not do anything. It's responsible for initialization of the fields
Rune FS
Debatable : I argue that the CLR does that prior to the constructor call, regardless of whether the constructor is default or not. You shouldn't mark me down unless you're clear of your facts.
Dr Herbie
@Dr. Herbie A class with an explicit initialization of a string with "" will look something like this in the default constructor:IL_0000: ldarg.0IL_0001: ldstr ""IL_0006: stfld string class Stackoverflow.Sorter`1<!T>::strIL_000b: ldarg.0IL_000c: call instance void mscorlib]System.Object::.ctor()where the last two lines are the call to the constructor of the base class (in this case System.Object)so the facts are that the default constructor initializes fields with a explicit initializer and calls the base class constructor I'm sorry but that's not nothing
Rune FS
+1  A: 

If you have the following class:

class A { }

There will be an empty constructor created by the compiler. If you add some field with initializations, like so:

class A 
{ 
    private string someField = "some text";
}

...the generated constructor body will contain the code to assign the value to the field.

Fredrik Mörk
and if no initialization is defined it will use default(T) where T is the type of the field
Rune FS
@Rune: if the field is not initialized in your code, there will be no sign of it in the generated constructor (it looks the same for `class A { }` and `class A { private string someField; }`).
Fredrik Mörk
@Frederik that's a matter of how you view it. When the constructor is called the memory for the object is initialized and if no initializer is defined the default will be used. This however is probably left to the CLR. and just to make it clear it was not a comment saying "your almost right" more your correct and this applies as well (from a functional view)
Rune FS
@Rune: Yes, I realized that after I posted my comment :) You are absolutely correct that the fields get initialized with their default values if no init code is given, but it is (as you indicate) handled by the CLR *before* the constructor is called.
Fredrik Mörk
@Frederik yes and initializers (might only be true for explicit initializers) and constructors in an inheritance chain are actually called in reverse order: http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspxbut worrying to much about initializers and constructor calls I find confuses more than it helps so I tend to view it simplyfied as 'constructor call' meaning everything that happen when the 'new' keyword is used. (Which of cause might be seen as a bit off compared to the question here)
Rune FS
A: 

If you do not define any constructors, a default one is created for you, but it doesn't do anything other than set your properties to their default values (as specified in your class definition, or by using the default operator). If you define a parameterless constructor, then it has whatever body you specify. If you define any constructors that take parameters, then you must manually define a parameterless constructor; the compiler will not create one for you in this case.

notJim
A: 

Each class that you define inherits from object. The default constructor body would be like this...

public object()
{
}

So if you have a class like this:

public sealed class Person
{
    public string Name = string.Empty;
    public string Firstname = string.Empty;
    public string Middlename = string.Empty;

    public string Fullname
    {
        get { return string.Concat(Name, Firstname, Middlename); }
    }

}

Then the constructor will look like this...

public Person()
{
    this.Name = string.Empty;
    this.Firstname = string.Empty;
    this.Middlename = string.Empty;
}

EDIT: If you use .NET Reflector you can disassemble the assemblies to have a look at what is really going on.

Yves M.