Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field initialization

Tags:

c#

.net

oop

Are there any differences between the following two ways of field initialization? When to use which one?

First way

public class Class1 {    private SomeClass someclass;     public Class1()    {        someclass = new SomeClass(some arg);    } } 

Second way

public class Class1 {    private SomeClass someclass = new SomeClass(some arg); } 

The field in the second example could be readonly.

like image 699
user137348 Avatar asked Dec 17 '09 09:12

user137348


People also ask

What is field initialization?

A field initializer allows you to initialize a field inline, instead of inside of a constructor. For example, instead of doing: class MyClass() { int a; int b; public MyClass() { a = 5; b = 3; } }

What is field initializer in Java?

The way to initialize class fields is with something called a static initializer. A static initializer is the keyword static followed by code in curly braces. You declare a class field much as you would declare a local variable. The chief difference is that field declarations do not belong to any method.

Do fields have to be initialized?

A required field must be initialized by the constructor, or by an object initializers when an object is created. You add the System.

What is initialization in Java?

In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code. Java offers two types of initializers, static and instance initializers.


1 Answers

You cannot make use of the this keyword when initializing fields inline. The reason for this is the order in which the code is executed: for all intents and purposes, the code to initialize a field inline is run before the constructor for the class (i.e. the C# compiler will prevent access to the this keyword). Basically what that means is this will not compile:

public class Class1 {    private SomeClass someclass = new SomeClass(this);     public Class1()    {    } } 

but this will:

public class Class1 {    private SomeClass someclass;     public Class1()    {        someclass = new SomeClass(this);    } } 

It's a subtle difference, but one worth making a note of.

The other differences between the two versions are only really noticeable when using inheritance. If you have two classes which inherit from each other, the fields in the derived class will be initialized first, then the fields in the base class will be initialized, then the constructor for the base class will be invoked, and finally, the constructor for the derived class will be invoked. There are some cases where you need to be very careful with this, as it could cause a fruit salad of complications if you don't realise what is going on (one of which involves calling a virtual method inside the base class constructor, but that is almost never a wise move). Heres an example:

class BaseClass {     private readonly object objectA = new object(); // Second     private readonly object objectB;      public BaseClass()     {         this.objectB = new object(); // Third     } }  class DerivedClass : BaseClass {     private object objectC = new object(); // First     private object objectD;      public DerivedClass()     {         this.objectD = new object(); // Forth     } } 

You will need to set breakpoints on all of the lines that initialize fields to be able to see the correct sequence.

like image 147
Rory Avatar answered Oct 08 '22 19:10

Rory