Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
16
Task/Singleton/C-sharp/singleton-1.cs
Normal file
16
Task/Singleton/C-sharp/singleton-1.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
public sealed class Singleton1 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: Yes
|
||||
{
|
||||
private static Singleton1 instance;
|
||||
private static readonly object lockObj = new object();
|
||||
|
||||
public static Singleton1 Instance {
|
||||
get {
|
||||
lock(lockObj) {
|
||||
if (instance == null) {
|
||||
instance = new Singleton1();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Task/Singleton/C-sharp/singleton-2.cs
Normal file
18
Task/Singleton/C-sharp/singleton-2.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public sealed class Singleton2 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: Yes, but only once
|
||||
{
|
||||
private static Singleton2 instance;
|
||||
private static readonly object lockObj = new object();
|
||||
|
||||
public static Singleton2 Instance {
|
||||
get {
|
||||
if (instance == null) {
|
||||
lock(lockObj) {
|
||||
if (instance == null) {
|
||||
instance = new Singleton2();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
Task/Singleton/C-sharp/singleton-3.cs
Normal file
6
Task/Singleton/C-sharp/singleton-3.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
public sealed class Singleton3 //Lazy: Yes, but not completely ||| Thread-safe: Yes ||| Uses locking: No
|
||||
{
|
||||
private static Singleton3 Instance { get; } = new Singleton3();
|
||||
|
||||
static Singleton3() { }
|
||||
}
|
||||
11
Task/Singleton/C-sharp/singleton-4.cs
Normal file
11
Task/Singleton/C-sharp/singleton-4.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
public sealed class Singleton4 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: No
|
||||
{
|
||||
public static Singleton4 Instance => SingletonHolder.instance;
|
||||
|
||||
private class SingletonHolder
|
||||
{
|
||||
static SingletonHolder() { }
|
||||
|
||||
internal static readonly Singleton4 instance = new Singleton4();
|
||||
}
|
||||
}
|
||||
6
Task/Singleton/C-sharp/singleton-5.cs
Normal file
6
Task/Singleton/C-sharp/singleton-5.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
public sealed class Singleton5 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: No
|
||||
{
|
||||
private static readonly Lazy<Singleton5> lazy = new Lazy<Singleton5>(() => new Singleton5());
|
||||
|
||||
public static Singleton5 Instance => lazy.Value;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue