Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Creating Lazy<T> - Singleton Class
WhatsApp
Mahadesh Mahalingappa
13y
12.1
k
0
0
25
Blog
Lazy<T> provides support for
Lazy Initialization
.
By Lazy Intialisation we mean that an object is not intialised until it is actually used. Below, I have shown how we can combine the Lazy<T> with the Singleton Pattern.
public sealed class LazySingleton
{
// Private object with lazy instantiation
private static readonly Lazy<LazySingleton> instance =
new Lazy<LazySingleton>(
delegate
{
return new LazySingleton();
}
//thread safety first
, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
private LazySingleton()
{
// no public default constructor
}
// static instance property
public static LazySingleton Instance
{
get { return instance.Value; }
}
}
Related Resources
Here is a list of some other related resources:
System.Lazy(Of T) Class
Lazy Initailization in .NET 4.0
Support for
Lazy Initialization
in .Net 4.0
Up Next
Create Object to Generic or Unknown Class
Ebook Download
View all
Coding Principles
Read by 2.7k people
Download Now!
Learn
View all
Membership not found