3

Recently I have been asked a question in an interview

Singleton pattern guarantees creating only a single object of a class in a process or app domain level?

Jaydeep Shil
  • 1,894
  • 22
  • 21
  • 1
    it is per appdomain as per my understanding – Ehsan Sajjad Nov 05 '19 at 13:12
  • I also told the same answer but interviewer were asking for reference to support my answer. – Jaydeep Shil Nov 05 '19 at 13:15
  • app domain is under CLR while process runs under OS, and if object is a managed then it's lifetime is with appdomain – Ehsan Sajjad Nov 05 '19 at 13:18
  • https://learn.microsoft.com/en-us/dotnet/framework/app-domains/application-domains#the-benefits-of-isolating-applications <- based on this section, I change my former comment to "App Domain". Silly question for an interview, though. – Fildor Nov 05 '19 at 13:18

1 Answers1

0

The C# specification about static fields states:

I.8.4.3 Static fields are always restricted to a single application domain basis (see §I.12.5), but they can also be allocated on a per-thread basis

You can get more information reading the CLI spec.

Gwinn
  • 1,218
  • 1
  • 9
  • 19
  • but its not necessary that static has been used to implement singleton. If I use private constructor ? – Jaydeep Shil Nov 05 '19 at 13:21
  • Can you give me an example ? Do you mean that you instance the object yourself and then keep it alive for the whole lifetime of the application ? – Gwinn Nov 05 '19 at 13:28