Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment in constructor vs direct assignment

Tags:

c#

a basic question popped in my head this morning. Here it is:

Is there any difference between doing this:

public class MyClass
{
    private object _myField = new object();

    ...
}

and doing the following:

public class MyClass
{
    private object _myField;

    public MyClass()
    {
        _myField = new object();
    }

    ...
}
like image 387
Frank Avatar asked Dec 10 '22 14:12

Frank


2 Answers

Yes, in the first, the field is initialized before the constructor call. In the second, the field is initialized during the constructor call.

like image 156
casperOne Avatar answered Dec 24 '22 12:12

casperOne


Just to demonstrate casperOne's point...

using System;

public abstract class BaseClass
{
    public BaseClass()
    {
        Console.WriteLine("Result for {0}: {1}", GetType(),
                          CalledByConstructor());
    }

    protected abstract string CalledByConstructor();
}

public class VariableInitializer : BaseClass
{
    private string foo = "foo";

    protected override string CalledByConstructor()
    {
        return foo;
    }
}

public class ConstructorInitialization : BaseClass
{
    private string foo;

    public ConstructorInitialization()
    {
        foo = "foo";
    }

    protected override string CalledByConstructor()
    {
        return foo;
    }
}


public class Test
{
    static void Main()
    {
        new VariableInitializer();
        new ConstructorInitialization();
    }
}

Here the base class constructor calls an abstract method implemented in the child class - this means we get to see the state of the object before its constructor body starts executing. The results are here:

Result for VariableInitializer: foo
Result for ConstructorInitialization:

As you can see, the variable initializer has already executed - but in the case where initialization only occurs in the constructor body, foo still has its default value of null.

Calling virtual methods from constructors is generally a very bad idea for precisely this sort of reason.

like image 29
Jon Skeet Avatar answered Dec 24 '22 14:12

Jon Skeet