im new in c# and i trying to learn a little more...Now, i have troubles understanding static ArrayList.
In php i can define:
Class Singleton{
private static $instance;
private static $arrayDemo = array();
private function __construct(){}
public static function getInstance(){
if(!isset(self::$instance))
self::$instance = new Singleton();
return self::$instance;
}
public static addItem($item){
self::$arrayDemo[] = $item;
}
public static getItems(){
return self::$arrayDemo[];
}
}
Singleton::getInstance();
Singleton::addItem("first");
Singleton::addItem("second");
Singleton::getItems(); // returns {0=>first,1=>second}
If i reload a page, i got same results (0=>first,1=>second)
Im trying to implement singleton pattern in c# to get same thing, but i got repeated values:
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private static ArrayList ArrayDemo = new ArrayList();
private Singleton() { }
public static Singleton Instance
{
get
{
return instance;
}
}
public static void AddItem(string item)
{
ArrayDemo.Add(item);
}
public static ArrayList GetItems()
{
return ArrayDemo;
}
}
//in cshtml:
Singleton.AddItem("first");
Singleton.AddItem("second");
Singleton.GetItems();
If i refresh website one time, i got same result as php... But, if i refresh it 3 times returns..:
0=>first,1=>second,2=>first,3=>second,4=>first,5=>second
Why this happens? I can clear ArrayList results in refresh if i used static method? I just want to understand the logic of this.
Thanks guys!.