Last active
January 15, 2026 20:46
-
-
Save sunmeat/527989ae327c8da76c71b54f14091e6a to your computer and use it in GitHub Desktop.
lazy initialization example C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Text; | |
| namespace LazyInitializationExample | |
| { | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.OutputEncoding = Encoding.UTF8; | |
| var lazy = new LazyHistory(); | |
| Console.WriteLine("Історія ще не створена!"); | |
| // виклик gethistory призведе до створення history | |
| History h1 = lazy.GetHistory(); | |
| h1.PerformAction(); | |
| // history уже створена і повторно не буде створена | |
| History h2 = lazy.GetHistory(); | |
| h2.PerformAction(); | |
| } | |
| } | |
| class LazyHistory | |
| { | |
| private History? h; | |
| public History GetHistory() | |
| { | |
| if (h == null) | |
| { | |
| h = new History(); | |
| } | |
| return h; | |
| } | |
| } | |
| class History | |
| { | |
| public History() | |
| { | |
| Console.WriteLine("Історія створена!"); | |
| } | |
| public void PerformAction() | |
| { | |
| Console.WriteLine("Виконання дії в історії."); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment