How do I use the singleton pattern in C#?

Learn how to create thread-safe instances with the singleton pattern in C#.

By Jeremy McPeak
February 21, 2017
Screen from "How do I use the singleton pattern in C#?" Screen from "How do I use the singleton pattern in C#?" (source: O'Reilly)

The singleton pattern, due to being limited to a single thread-safe instance and its last minute instantiation, has a reputation for being cumbersome. Using Visual Studio, Jeremy McPeak demonstrates how to create a class limited to one instance, and seal the singleton for improved runtime performance. Jeremy then shows you how to expose the private instance class variable using a static property. Novice C# beginners will be able to implement the singleton pattern correctly and make their classes thread safe.

Grow your C# skills with the C# Learning Path on Safari.

Learn faster. Dig deeper. See farther.

Join the O'Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

Learn more

Jeremy McPeak is a self-taught programmer who began his career tinkering with websites in 1998. He is the author of Beginning JavaScript, 5th Edition, JavaScript 24-Hour Trainer, and co-author of Professional Ajax, 2nd Edition. He also contributes to Tuts+ Code (http://code.tutsplus.com), providing articles, video tutorials, and courses on JavaScript, C#, ASP.NET, and PHP. He is currently employed by an oil and gas company, building in-house conventional and web applications.

Post topics: Software Engineering
Share: