Question

Visual studio 2012 is outputing the indicated compiler error message when I attempt to compile the following code:

        string choice;
        var inputs = new ArrayList();

        do
        {
            Console.Write("enter a score:");
            int score = Int32.Parse(Console.ReadLine());
            inputs.Add(score);

            Console.WriteLine("do you want to enter more scores? y for yes and n for no");
            choice = Console.ReadLine();
            if (choice == "n") { break; }

            while (choice != "y" && choice != "n")
            {
                Console.WriteLine("Please enter a valid response!");
                choice = Console.ReadLine();
            }

        }
        while (choice == "y");


        int sum = 0;
        for (int i = 0; i < inputs.Count; i++)
        {
            sum += inputs[i];
        }

compiler error: Operator += cannot be applied to operands of type 'int' and 'object'

However, if I compile the following I get no such error message:

 int[] arr = new int[] { 1, 2, 3 };
        int sum = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            sum += arr[i];
        }
Was it helpful?

Solution

If you use ArrayList, the type of inputs[i] is Object. The compiler is telling you (quite appropriately) that it can't use += when you're using int and Object.

In your second example, of course, you're using an int[], so there's no problem.

You could probably use a cast to get around this, but I would use List with an int type parameter instead:

var inputs = List<int>();

OTHER TIPS

You need to supply a type to your ArrayList. Like so: var inputs = new List<int>();

Without explicitly stating which kind of objects you want to store in your list, the compiler will treat it as a List<object>.

inputs is an ArrayList, which means it contains objects. You need to cast to int:

(int)inputs[i];

But why not use a List<int> instead? ArrayList is a leftover from C# 1.0 No one wants to use that

An Arraylist is always only known to be a list of object - in this case it is indeed an integer - but this is not know at run-time.

There are two ways to solve this:

the best, would be to change your ArrayList to use generics (recommended):

var inputs = new List<int>();

or, cast at the point of usage:

sum += (int)inputs[i];

ArrayList is an untyped list of objects. Objects you add to such collection don't need to be homogeneous (it can hold few int, few float, few strings and one Cat). you have to cast object you extract to their proper type (of course because compiler doesn't know how to add int with object unless you're using dynamic):

for (int i = 0; i < arr.Length; i++)
{
    sum += (int)inputs [i];
}

That said I'd suggest to use a strongly typed list (for example List<T>) instead of ArrayList, like this:

var inputs = new List<int>();

Why? Because with ArrayList nothing stops you to write (on purpose or because of a bug) this code:

inputs.Add(123);
inputs.Add("123");
inputs.Add(new Version("1.0"));

Obviously this will fail at run-time when wrong object type will be added to your list, this constraint (list of integers) is known at compile-time so it's much better to enforce it. Moreover you'll avoid a cast but performance gain is so small that it'll go unnoticed for sure.

This is because inputs is an ArrayList:

var inputs = new ArrayList();

The compiler doesn't know about the type of the array item since array list is not typed

You would need to convert the element to an int or just use a List<int> which is preferable IMO:

int sum = 0;
for (int i = 0; i < inputs.Count; i++)
{
    sum += (int)inputs[i];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top