Take the 2-minute tour ×

In Java instance variables can be initialized by an initialization block as shown below:

class Example {
    private int varOne;
    private int varTwo;

        // Instance Initializer
        varOne = 42;
        varTwo = 256;

Is there an equivalent construct in C#?

[Edit] I know that this can be inline with the instance variable declaration. However, I am looking for is something similar to the static constructor in C# but for instance variables.

share|improve this question
Point of pedantry: instance initialiser blocks and instance field initialisation are executed by the constructor immediately after calling super. –  Tom Hawtin - tackline Mar 28 '09 at 13:05
@Tom - You are right about the calling order. The source code comment is removed. –  maxyfc Mar 28 '09 at 13:16
Why not just make it a method and call it from the constructor, if that is what happening behind the scene anyway? –  toxvaerd Mar 28 '09 at 13:19
@toxvaerd - Sometimes we can't create constructors (e.g. anonymous class in Java). But, I think you can use different constructs like delegates in C# to do what anonymous classes are used for in Java (e.g. GUI event handlers). Just wondering if there is an equivalent construct. –  maxyfc Mar 28 '09 at 13:35
add comment

2 Answers

up vote 5 down vote accepted

There really is no equivalent in C#. C# has only 2 ways to initialize instance varibles

  1. In the constructor
  2. By explicitly initializing the variable at it's declaration point

There is no way to do an initialization after the object is created but before the constructor runs.

share|improve this answer
add comment

Create an instance constructor that any other local constructor will call in the initialization list:

private Example ()
    //initialize all fields here

public Example (/*list of parameters*/) : this()
    //do specific work here

If the default constructor is already required by the logic of the application, then susbstitute

private Example ()


private Example (object dummy)

and, of course, accordingly modify the initilization call.

share|improve this answer
unfortunately that doesn't let you have multiple instance initializer blocks... but for the common case would be reasonable. –  TofuBeer Mar 28 '09 at 16:34
Curious - what is TofuBeer? and also, why would you want multiple instance initializer blocks? For the latter - it seems it would lead to confusion with blocks scattered throughout the class code, separate from constructors and separate from the member decls. –  Cheeso Mar 28 '09 at 22:26
a concrerte example - there is a private readonly field I need to initialize based on some class methods that are unavailable at the static initialization. –  Roland Tepp Feb 10 '11 at 8:59
In Java (static/instance) initialization block is usually (!) used in conjunction with static/instance variables that need some sort of special initialization incantation. Using initializer blocks allows you to keep the initialization code close to the fields being initialized this way, thus lessening the cognitive distance between declaring something and setting up it's value. It's not perfect, but sometimes necessary –  Roland Tepp Feb 10 '11 at 9:03
add comment

Your Answer


