質問

What is the difference between the two cases below:

class Data
{
    PersonDataContext persons = new PersonDataContext();
    public Data() {}
}

versus

class Data
{
    PersonDataContext persons;
    public Data() 
    {
        persons = new PersonDataContext();
    }
}

I have the same question in asp.net:

public partial class Data : System.Web.UI.Page
{
    PersonDataContext persons = new PersonDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

versus

public partial class Data : System.Web.UI.Page
{
    PersonDataContext persons;
    protected void Page_Load(object sender, EventArgs e)
    {
        persons = new PersonDataContext();
    }
}
役に立ちましたか?

解決 2

Field initializers runs before the constructor call. So In Case 1 your object of PersonDataContext will be created before the constructor call.

PersonDataContext persons = new PersonDataContext();

In 2nd case, your constructor will execute first and then it will initialize the field.

You can do a simple test

class PersonDataContext
{
    public PersonDataContext()
    {
        Console.WriteLine("In PersonDataContext Constructor");
    }
}
class Data
{
    PersonDataContext persons = new PersonDataContext();
    public Data() 
    {
        Console.WriteLine("In Data Contructor");
    }
}

class Data2
{
    PersonDataContext persons;
    public Data2() 
    {
        Console.WriteLine("In Data2 Constructor");
        persons = new PersonDataContext();
    }
}

and then in your Main method

static void Main(string[] args)
{

    Data d1 = new Data();
    Data2 d2 = new Data2();
    Console.Read();
}

Output

In PersonDataContext Constructor // Case 1

In Data Contructor //Case 1

In Data2 Constructor //Case 2

In PersonDataContext Constructor // Case 2


For the asp example, In first case your Field is initialized before the Page_Load event, and in 2nd case your Field is initialized in the Page_Load event

他のヒント

In the first case you initialize the variable at the site of declaration.

In the second, you initialize it in a constructor.

The main difference is that if you have additional constructors and either forget to initialize or chain the constructors, you can have an uninitialized variable.

In your second example, the main difference is that Page_Load is just one of many ASP.NET events that are called when processing the request. As a result, events raised before Page_Load will not be able to access/use this field, because it's not yet been initialized.

Check the ASP.NET Page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx

The only difference is the order at which the objects are created. If I remember well, fields initializer are run before the constructor, so that if the constructor referenced the field, it's already there.

But from 99.9% of the case, you won't see a different between the two. In most case, it's safer to initialize your field directly on it, it will prevent you from forgetting it in case of multiple constructor or derived class and complex hierarchy.

Often, constructors are used to take outside parameters to populate fields that could not be with field initializer.

You will have unintialized variable for second case if you use constructor other than default constructor for object initialization -

Data data = new Data(5);

But if you chain parametized constructors to call default constructor like this -

class Data
{
    PersonDataContext persons;
    public Data() 
    {
        persons = new PersonDataContext();
    }

    public Data(int a) : this()
    {

    }
}

variable persons will be initialized.

In your first example there will be no effective difference as long as the Constructor is the only Constructor. If you add other Constructors then you need to make sure they all initialize the 'persons' field.

In the ASP.NET example (for the second code snippet) - you're initialising the 'persons' field in the Page_Load method, which means that any other methods that use this field might find it uninitialized. (Did you mean Page_Load, or did you mean to make it a Constructor...?)

The bottom line is, if 'persons' field always needs to be non-null, it's safer to initialise as a Field Initialiser...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top