Skip to content

Instantly share code, notes, and snippets.

@surrealist
Created October 5, 2018 17:17
Show Gist options
  • Select an option

  • Save surrealist/4b643164d5f881c615110d500324bcf6 to your computer and use it in GitHub Desktop.

Select an option

Save surrealist/4b643164d5f881c615110d500324bcf6 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
namespace Oct5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Counter.Instance.NextId);
Console.WriteLine(Counter.Instance.NextId);
Console.WriteLine(Counter.Instance.NextId);
}
}
class Counter
{
private static Lazy<Counter> _instance
= new Lazy<Counter>(() => new Counter());
private Counter() { }
public static Counter Instance
=> _instance.Value;
private int _nextId = 0;
public int NextId => Interlocked.Increment(ref _nextId);
}
}
@surrealist
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment