Join Reynald Adolphe for an in-depth discussion in this video Related objects needed sometimes, part of C# Best Practices for Developers.
- Now for our final scenario,…we're considering when we need an object sometimes.…There's no need to use a constructor…as it's going to be a waste.…Why create an instance every time…when we're only going to use an object occasionally?…So we should wait and create it when we really need it.…And that's when lazy loading comes into play.…So in our default constructor let's go ahead…and comment out where we are creating a new instance…of the agency and instead let's modify our property…by adding an if clause.…
I'll type in if and use that snippet by hitting tab, tab…and then enter in if the current agency is null…and at this point we can create a new instance…for that currency.…A new instance of an agency.…If it's not the case…it's going to return the current instance.…So let's go ahead and save that…and we'll run our test one more time.…
And it looks like it worked successfully.…And this is ideal, once again,…for when we know we're going to need an agency occasionally…and not create an instance every single time.…So to touch on best practices, so just keep in mind…
- 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.