Task 1
Write a thread-safe singleton in C#: one object for the whole app, created only when first used. Then say when you'd use a DI singleton instead.
Publicis Sapient, tech round, April 2026: singleton implementationStuck? Hints
- How do you stop other code writing
new RateTable()? (a private constructor) - is a .NET type that creates its value once, the first time you ask, and is safe when two threads ask together.
- Why mark the class ?
Show a solution
Lending.sln · where this file sits
- Private constructor, so fails outside the class.
static readonly Lazy<RateTable>: created on first use, thread-safe by default.- Class is : it states the intent, and closes the last gap, a nested class inheriting it.
- No hand-written (the older double-checked locking pattern); does it for you.
- Says the DI answer:
AddSingleton<IRateTable, RateTable>()gives one instance plus an interface, so tests can fake it.