1

I saw this expression as a short and thread safe to implement a singleton design pattern:

public static MySimpleSingleton Instance { get; } = new MySimpleSingleton();

What is the meaning of this part in the expression ? Is it kind of coalesce ? What is the formal name of it ?

Instance { get; } = new MySimpleSingleton()
Liam
  • 27,717
  • 28
  • 128
  • 190
Guy E
  • 1,775
  • 2
  • 27
  • 55
  • 4
    That's an [auto-implemented property initialiser](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties), which was introduced in C#6. – Johnathan Barclay Jan 20 '22 at 08:53
  • 2
    Does this answer your question? [What is the best way to give a C# auto-property an initial value?](https://stackoverflow.com/questions/40730/what-is-the-best-way-to-give-a-c-sharp-auto-property-an-initial-value), [Automated property with getter only, can be set, why?](https://stackoverflow.com/q/34743533/8967612) – 41686d6564 stands w. Palestine Jan 20 '22 at 08:54
  • Note that C# 9 allows you to shorten this even further with the use of target-typed `new`, allowing just `MySimpleSingleton Instance { get; } = new()`. – Jeroen Mostert Jan 20 '22 at 08:56

0 Answers0