Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I read that you cannot instantiate a static class.However here i am instantiating Array List and surprisingly for me C# has no problems.Also each time my main loop runs i am creating a new instance of the class "TryTo" and adding to it an integer value.Since i created a new instance each time of my class "tryTo" i expected new static array list created for each instance.Thus i expected 10 Array Lists each containing only one value at the end of for loop of main.So, i expected a blank display at the end.But the output of elements of ArrayList was 0...4...9 as if all the time i had been working with only one instance of class.

The code is very very simple you will get my question in no time.


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            for(i=0;i<10;i++)
            {
                new TryTo().add(i);//Add current loop value to array List of                     
            }
            new TryTo().display();//display array list elements of newTryTo()
        }
    }
    public class TryTo
    {
        //static array list inside a non-static class
        static System.Collections.ArrayList aL=new System.Collections.ArrayList();
        public void add(int i)
        {
            aL.Add((object)i);
        }
        public void display()
        {
            foreach(object i in aL)
            {
                //displays objects in  array List for that instance
                Console.WriteLine((int)i);
                //runs only once when new TryTo.display() is called
                //hence i expected it to conatin zero elements but instead found
                //it to contain elements of previous instances
            }
            Console.Read();
        }
        

    }
}
Posted
Updated 24-Jun-15 3:24am
v2

That is how static variables work.

"aL" is a static variable on type TryTo. You only have one class called TryTo so there is only one aL variable (we'll call it TryTo.aL).

In this code

C#
TryTo t1 = new TryTo();
TryTo t2 = new TryTo();
TryTo t3 = new TryTo();


t1, t2 and t3 are instances of TryTo, and they have individual copies of all non-static fields. However when any of them interact with "aL" they are interacting with TryTo.aL, and there is only one TryTo.aL so every instance uses the same aL, therefore the same list.

The thing that can easily confuse is this line;

C#
static System.Collections.ArrayList aL=new System.Collections.ArrayList();


you'd be forgiven for thinking that this initialisation happens every time you create a new instance of TryOne, but it doesn't, .net treats static instance declarations in a special manner and no matter how many times you create an instance of TryOne, the "= new ArrayList()" is only called the first time aL is used, this line is not called on each instance creation.

I've re-arranged your code a little, this might make it more obvious what is happening

C#
class Program
{
    static void Main(string[] args)
    {
        TryTo t1 = new TryTo();
            
        // t1 is an instance of TryTo and has its own copy of alNonStatic
        t1.aLNonStatic.Add(1); // "1"
            
        // This line doesn't compile, there *is* no alStatic on t1 as t1 is an instance of TryTo
        // and aLStatic is on the type, not the instance
            
        //t1.aLStatic.Add(1);
            
        // this is how we access aLStatic
        // the fact that you are referencing the variable on the type and not the
        // instance is hidden in your code
        TryTo.aLStatic.Add(1); // "1"

        // when we call the line above, aLStatic is given the "new ArrayList"
        t1.display();

        TryTo t2 = new TryTo();
            
        // t2 is an instance of TryTo and has its own copy of alNonStatic
        t2.aLNonStatic.Add(2); // "2"
            
        // again there is no t2.aLStatic, only TryTo.aLStatic
        TryTo.aLStatic.Add(2); // "1", "2"

        // when we call the line above as aLStatic has already been initialised the "new ArrayList" is not called
        t2.display();
    }
}

public class TryTo
{
    public static System.Collections.ArrayList aLStatic = new System.Collections.ArrayList();
    public System.Collections.ArrayList aLNonStatic = new System.Collections.ArrayList();

    public void add(int i)
    {
        aLStatic.Add((object)i);

        // the above is really the line below
        // this makes it more obvious that alStatic does not belong to the class instance but the class type
        //TryTo.aLStatic.Add((object)i);
    }
    public void display()
    {
        Console.WriteLine("Static");
        foreach (object i in aLStatic)
        {
            Console.WriteLine((int)i);
        }

        Console.WriteLine("Non Static");
        foreach (object i in aLNonStatic)
        {
            Console.WriteLine((int)i);
        }

        Console.ReadLine();
    }


}
 
Share this answer
 
Quote:
I read that you cannot instantiate a static class.However here i am instantiating Array List and surprisingly for me C# has no problems.
You are not instantiating a static class, you are instatinating an non-static class having a static member variable (the aL list).


Quote:
ince i created a new instance each time of my class "tryTo" i expected new static array list created for each instance.Thus i expected 10 Array Lists each containing only one value at the end of for loop of main
That's wrong. Being static, aL is shared among all the instances of the TryTo class (and the new statement is executed exactly once). Each time an instance of the TryTo accesses 'its' aL, it actually accesses the shared member.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900