Provides lazy initialization routines.
Assembly: mscorlib (in mscorlib.dll)
|EnsureInitialized<T>(T)||Initializes a target reference type with the type's default constructor if it hasn't already been initialized.|
|EnsureInitialized<T>(T, Func<T>)||Initializes a target reference type by using a specified function if it hasn't already been initialized.|
|EnsureInitialized<T>(T, Boolean, Object)||Initializes a target reference or value type with its default constructor if it hasn't already been initialized.|
|EnsureInitialized<T>(T, Boolean, Object, Func<T>)||Initializes a target reference or value type by using a specified function if it hasn't already been initialized.|
These routines avoid needing to allocate a dedicated, lazy-initialization instance, instead using references to ensure targets have been initialized as they are accessed.
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
The following example demonstrates how to use EnsureInitialized to lazily initialize a value using a Boolean value to track whether initialization has already happened and an object to use as the mutual exclusion lock.
.NET FrameworkSupported in: 4.5, 4
.NET Framework Client ProfileSupported in: 4
Portable Class LibrarySupported in: Portable Class Library
.NET for Windows Store appsSupported in: Windows 8
.NET for Windows Phone appsSupported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.