views:

39

answers:

1

Hello!

I was wondering when I write

Shared ReadOnly Variable As DataType = New DataType()
Or alternatively
Shared ReadOnly Variable As New DataType()

Is it lazy loaded or as the instance initializes?

How about ReadOnly itself without the shared?

Example: System.Type.Delimiter

+2  A: 

Neither is lazy loaded.

They are initialized when the Type is initialized: static initialization is triggered when any static member is accessed or the first time an instance of the type is created.

The example you mention (System.Type.Delimiter) is initialized by a Static Constructor.

Joe
When I declare a shared readonly member in a regular class, when is it initialize?
Shimmy
It is initialized _no later_ than any method on the class (shared or instance, including special methods such as constructors) is invoked. It can be initialized earlier at any time, at runtime's discretion. If you have a static initializer in the class, then the guarantee is strong: initializers for fields will _always_ execute at the moment of first call of any method of the class (including ctors).
Pavel Minaev