1

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!.

pmtamal
  • 2,157
  • 1
  • 12
  • 7
Carasuman
  • 636
  • 1
  • 7
  • 17
  • that's the issue: php doesn't know *real* static instances :) see http://stackoverflow.com/questions/468642/is-it-possible-to-create-static-classes-in-php-like-in-c/468653#468653 and http://stackoverflow.com/questions/432192/singleton-in-php –  Oct 23 '12 at 06:12
  • 1
    @AndreasNiedermair As I learn more from C #, I felt I lost 5 years in php world :( – Carasuman Oct 23 '12 at 06:14

1 Answers1

3

Why this happens?

It's a static variable. It lives for as long as the AppDomain it's a part of lives - which will almost always be significantly longer than a single request, which appears to be what you were assuming the lifetime would be. In ASP.NET, the same AppDomain serves many requests - you don't get a separate process for each request.

I would suggest that:

  • You avoid using the singleton pattern to start with; it's very rarely a good idea
  • You avoid using static variables other than for data which is effectively constant (and possibly even then...)
  • You avoid using the non-generic collections like ArrayList, instead favouring the generic collections such as List<T>

Additionally, if you're new to C# I'd suggest you discard the web part to start with - both web and rich client GUI frameworks have their own quirks to learn about; if you want to experiment with the language and the fundamental core libraries (collections and IO, for example) I find it much more productive to use console apps for this purpose.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I will follow your advice .. As I go along, I feel I'm missing much more to understand and learn. Btw, if I seek to handle variables from a single instance, is not recommended singleton pattern? – Carasuman Oct 23 '12 at 06:19
  • @Carasuman: The singleton pattern leads to code which is hard to test, and particularly unsuitable for mutable data within a web application. Dependency injection usually helps to provide the same instance to multiple objects. It really depends on what you're trying to do though. – Jon Skeet Oct 23 '12 at 06:21
  • You suggest a book to start from scratch in C #? Thanks for your help – Carasuman Oct 23 '12 at 06:23
  • @Carasuman: C# in a Nutshell is good, or if you prefer a Head-First style, the *second* edition (or later) of Head-First C# is okay. It's a bit GUI-focused for me, but hey... – Jon Skeet Oct 23 '12 at 06:25