Join Reynald Adolphe for an in-depth discussion in this video Using a singleton, part of C# Best Practices for Developers.
- [Instructor] A singleton is a common design pattern,…and you can actually learn more about it…in my Design Patterns course.…It is essentially a class…that provides one instance.…So for example, say you have a desktop application…that manages insurance policies.…When someone logs in,…they may need to deal with their one policy…to read, delete, or modify.…So we can define the policy as a singleton…during the entire execution of the application.…
As you can see here,…let's walk through the code,…starting on line 22,…where we define the class.…We move on down to where we have…our static Policy called _instance.…On line 26, we have the accessibility modifier…which is set to private,…to prevent others from accessing it.…On line 28 through 35,…we've added a static property…that creates the instance…if, and only if one doesn't already exist,…and that instance can be accessed…by typing in Policy.instance.…
Now, this is a fairly straightforward example…of a singleton,…but for a more detailed explanation,…actually using this example,…
- Naming and handling classes
- Running a unit test
- Using constructors
- Naming and handling methods, fields, and properties
- Using properties correctly
- Managing objects efficiently
- Common design patterns as best practices
Skill Level Intermediate
1. Best Practices: An Overview
2. Naming and Handling Classes
3. Naming and Handling Methods, Fields, and Properties
4. Managing Objects Efficiently
5. More Best Practices Resources
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.