Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active January 15, 2026 20:46
Show Gist options
  • Select an option

  • Save sunmeat/527989ae327c8da76c71b54f14091e6a to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/527989ae327c8da76c71b54f14091e6a to your computer and use it in GitHub Desktop.
lazy initialization example C#
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