Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungRefreshed repository pull requests page generally available(22.09.2026 um 03:25 Uhr)
Sichere ProgrammierungThe Joy of Learning the Basics Again(22.09.2026 um 03:28 Uhr)
Sichere ProgrammierungZero-Code OpenTelemetry Tracing for Dagster(22.09.2026 um 03:39 Uhr)
Linux Tipps & Hardening`prime-all`(22.09.2026 um 02:28 Uhr)
IT Security Toolsopensoho v0.15.2(22.09.2026 um 03:33 Uhr)
IT Security NachrichtenUS Proposes AI Incident Alert System in Talks With China, Bessent Says(22.09.2026 um 04:01 Uhr)
Sichere ProgrammierungRefreshed repository pull requests page generally available(22.09.2026 um 03:25 Uhr)
Sichere ProgrammierungThe Joy of Learning the Basics Again(22.09.2026 um 03:28 Uhr)
Sichere ProgrammierungZero-Code OpenTelemetry Tracing for Dagster(22.09.2026 um 03:39 Uhr)
Linux Tipps & Hardening`prime-all`(22.09.2026 um 02:28 Uhr)
IT Security Toolsopensoho v0.15.2(22.09.2026 um 03:33 Uhr)
IT Security NachrichtenUS Proposes AI Incident Alert System in Talks With China, Bessent Says(22.09.2026 um 04:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Implementing Thread Safe Singleton Pattern in C#

Singleton (Thread Safe) private static readonly Lazy<DbConnectionThreadSafe> _instance = null!; Using Lazy<T> Because Thread safe by default Provides lazy initialization static…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




Singleton (Thread Safe)






private static readonly Lazy<DbConnectionThreadSafe> _instance = null!;









Using Lazy<T> Because




  • Thread safe by default

  • Provides lazy initialization




static DbConnectionThreadSafe()
{
_instance = new Lazy<DbConnectionThreadSafe>(
() => new DbConnectionThreadSafe(_connectionString),
LazyThreadSafetyMode.ExecutionAndPublication
);
}






Let's break down this static constructor and the Lazy initialization:




static DbConnection()  // This is a static constructor
{
_lazyInstance = new Lazy<DbConnection>(
() => new DbConnection(_connectionString), // Lambda expression
LazyThreadSafetyMode.ExecutionAndPublication // Thread safety mode
);
}






Let's examine each part:





  1. Static Constructor (static DbConnection())




    • A static constructor is called automatically before any static members are referenced

    • It runs only once per app domain

    • It's thread-safe by default (CLR guarantees this)

    • Used to initialize static fields




  2. Lazy





    • Lazy<T> is a class that provides lazy initialization

    • The actual object isn't created until it's first accessed

    • When you access Lazy<T>.Value for the first time, it creates the instance




  3. Lambda Expression (() => new DbConnection(_connectionString))




    • This is a factory function that creates the DbConnection instance

    • It's only executed when the Lazy.Value is first accessed

    • The => syntax defines a lambda expression (anonymous function)




  4. LazyThreadSafetyMode.ExecutionAndPublication




    • This enum value specifies how the Lazy instance handles thread safety

    • Three possible modes:






// No thread safety
LazyThreadSafetyMode.None

// Locks initialization
LazyThreadSafetyMode.ExecutionAndPublication

// Multiple threads can attempt initialization
LazyThreadSafetyMode.PublicationOnly






The execution flow:




  1. When the class is first referenced, the static constructor runs

  2. The _lazyInstance is created, but the DbConnection instance is not yet created

  3. When Instance is first accessed:


    • The lambda expression runs

    • Creates new DbConnection with the connection string

    • Stores it in the Lazy instance



  4. Subsequent accesses to Instance return the same stored instance

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Implementing Thread Safe Singleton Pattern in C#

Thematisch verwandte Begriffe: Implementing, Thread, Safe, Singleton · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick