[C#] System.Array

Associate
Joined
1 Feb 2006
Posts
1,868
Location
Reading
Code:
public static void Delete(System.Array aArray, int iIndex)
        {
            ...
            for (int i = iIndex; i < iArrayLength; i++)
            {
                aArray[i] = aArray[i + 1];             
            }
            ...           
        }

Above is a snippet of code, it generates the error:

"Cannot apply indexing with [] to an expression of type 'System.Array'"

How can i get around this, it needs to be able to handle arrays of different types.
 
Make the function generic:
Code:
public static void Delete<T>(T[] aArray, int iIndex)
{
    // ...
    for (int i = iIndex; i < aArray.Length; i++)
    {
        aArray[i] = aArray[i + 1];
    }
    // ...
}

// ...

public static void Foo()
{
    int[] bar = new int[] { 0, 1 };
    Delete<int>(bar, 0);
}
 
Last edited:
Inquisitor said:
Make the function generic:
Code:
public static void Delete<T>(T[] aArray, int iIndex)
{
    // ...
    for (int i = iIndex; i < aArray.Length; i++)
    {
        aArray[i] = aArray[i + 1];
    }
    // ...
}

// ...

public static void Foo()
{
    int[] bar = new int[] { 0, 1 };
    Delete<int>(bar, 0);
}

The generic serves no purpose here, so why bother use it? Just use an Object[] :


Code:
public static void Delete(Object[] aArray, int iIndex)
{
    // ...
    for (int i = iIndex; i < aArray.Length; i++)
    {
        aArray[i] = aArray[i + 1];
    }
    // ...
}

// ...

public static void Foo()
{
    int[] bar = new int[] { 0, 1 };
    Delete(bar, 0);
}

By the way - your indexing off the end of the array - so your going to get an index out of bounds exception as well.

Lastly: If you need to implement something like this you should be using an ArrayList, which does this for you. No need to re-invent the wheel.
 
Last edited:
Lagz said:
The generic serves no purpose here, so why bother use it? Just use an Object[]
You can't convert between array types though; that code snippet would generate a compile time error.

Although you're right, it would be much more efficient to use a list in this case. :)
 
I wouldn't use an implicit Object[] cast as that will cause the whole array to be boxed which is a slow operation by itself, but multiplied by O(n) and it's even worse.

Use a collection, preferably a generic one such as List<>. ArrayList is slow. Type safety is your friend, not an enemy!
 
Last edited:
Please bare with me as im new to c#.

Say if you had an arraylist of objects. How would you access an elements members. Because at compile time the type of objects held by the arraylist is unknown. So if I try:

ArrayList aArray;

aArray[1].index

I get an error "'object' does not contain a definition for 'index'"

Thanks for help ppl.
 
What version of C# are you using?

If you're using C# 2.0 then as said by NathanE you should use the generic List<> rather than ArrayList.
This lets you define the type contained in the list so you can know this at compile time.

Have a read up on Generics in .NET if you're unsure about this.
 
Back
Top Bottom