.Net 4.0 has come up with Lazy initialization class, meaning initialize when needed. So when object is declared, memory for object is not allocated until it is actually needed. This class can be used for any custom type as you can see the in title Lazy(Of T)
Lets see this, suppose we have following class.
Public Class person
'Remember Property can be defined in one line now
Public Property Age As Integer
Public Sub New(ByVal _Age As Integer)
Age = _Age
End Sub
'To work with Lazy class, we need New without constructor, oh...
Public Sub New()
Age = 45
End Sub
End Class
Now, A test with Lazy Class
Dim person1 As New System.Lazy(Of person)
'Check whether its created
Debug.WriteLine("Value created before Use?: {0}", person1.IsValueCreated)
Debug.WriteLine("Person's Age: {0}", person1.Value.Age)
'Lets Check Again, it must be created since value was needed in above line
Debug.WriteLine("Value created after Use?: {0}", person1.IsValueCreated)
'To assign the age
person1.Value.Age = 56
Debug.WriteLine("New age {0}", person1.Value.Age)
721050b2-b95d-42eb-bae0-0b3058111165|0|.0