c# singleton
using System;
namespace DesignPattern.Singleton
{
class Program
{
var printer1 = Printer.GetInstance();
var printer2 = Printer.GetInstance();
Console.WriteLine(printer1.GetHashCode()); //output 20538874
Console.WriteLine(printer2.GetHashCode()); //output 20538874
//both printer1 and printer2 has the same hash code
//that is the same instance
}
public class Printer
{
private static Printer instance;
private Printer(){}
public static Printer GetInstance()
{
if (instance == null)
{
instance = new Printer();
}
return instance;
}
}
}public class SingletonOnDemand {
private SingletonOnDemand () {}
private static class Singleton {
private static final SingletonOnDemand instance = new SingletonOnDemand();
}
public static SingletonOnDemand getInstance () {
System.out.println("create instance");
return Singleton.instance;
}
}
Also in C#:
- how to convert object in string JSON c#
- c# serialize
- print line in python
- list.addrange in c#
- c# switch case with or condition
- ASP select box all states
- How to get number of months between 2 dates c#
- string isnullorempty vs isnullorwhitespace
- random from list c#
- viewBag as a list
- c# null conditional
- c# random number between 0 and 1
- c# how to use inovke
- constructor in inherited class c#
- how to update a project to cross target .net core
- Movement 2d unity
- c# post request
- C# get all child classes of a class
- unity on mousewheel down
- how to get component in unity c#
- Generate Genealogy view in mvc C# using Google Organizational Chart
- unity how to add a bullet impact force
- unity cycle children
- .net loop through dictionary