Created
October 5, 2018 17:17
-
-
Save surrealist/4b643164d5f881c615110d500324bcf6 to your computer and use it in GitHub Desktop.
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; | |
| 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); | |
| } | |
| } |
Author
surrealist
commented
Oct 5, 2018

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