• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to use Array type in C#

elkinm

Platinum Member
I need to use Arrays of multiple types for my program.
I tried to use:

Array testarray = new Array[2]
testarray.SetValue("value1",0);
testarray.SetValue(15,1);

this compiles, but creates an exception when trying to set the values.

I have to use:

Object[] testarray = new Object[2]

And everything works just fine.
I would thing the two should be identical as I have used Array to read values from an array with casting.

What kills me is that the code with Array compiles and I cannot find a single reference or example of the actual Array class everything I have found is always of a specific type.

Thanks
 
Array testarray = new Array[2]
Means you've created a 2D array with two elements of type Array. So you can only put objects of type Array in testarray.

Object[] testarray = new Object[2]
Means you've created a 2D array with two elements of type Object. So you can only put objects of type Object in testarray. Since everything is an object (I think primitives will get autoconverted to their Object form), you can store whatever you want in testarry.
 
There is no array class. Arrays are collections of objects, nothing more. Objects can be ints, grandmas, anything that is declared as a class.

Also look at generic collections, they can be useful because they provide some powerful functionality.

Look up an array tutorial, there are hundreds.
 
Thanks.

When I get values from a passed in array I use Array.

Array = (Array)sentarray;

string val1 = (string)sentarray.GetValue(0);
int val2 = (int)sentarray.GEtValue(1);

Basicly I am taking a generic array of unknown size and reading the values.

I expected to be able to create the Array like an ArrayList where I can add any objects after array creation.

Every Array tutorial I have found uses specific type or object arrays, I have not found even one that lists reading from an array as I did above.
Can someone point me to one, but one just covering "Array" as mentioned above, possibly in a comparison vs Object[] arrays.

Thanks again.
 
Why can't you use Object[]? It does exactly what you want. It's an array where each element can be of any type.

Array x = new Array[2]
Is an array of Arrays. Also x.setvalue isn't going to work because the element you refer to in the 2nd arg is an uninstantiated object in your example. You never did

x[0] = new Array()
Which I'm pretty sure won't compile because I think Array is abstract.
 
I searched for "Array Class" many times and never found this specific MSDN page.

It does list Array myArr = Array.CreateInstance( as a way to create an Array of objects. Still does not read from an array like I did but it still the best description of the class I found to date.

Also, wouldn’t I need:

Array[] x = new Array[2]

to create an array of arrays or is the [] irrelevant here.

Either way it seems way to complicated so I will use Object[] as always. It has always bothered me that I cannot use Array for generics.
Especially since I used ArrayList x = new ArrayList() without problems.

I certainly have much more information about Array than I had before.

Thank you.
 
Last edited:
Also, wouldn’t I need:

Array[] x = new Array[2]

Yes, you're right. I overlooked that the [] was missing and now I see what you were trying to do. And no, that wouldn't work. I'm surprised that it even compiled.

Although I'm still not sure why you saw Array to be advantageous over Object[] or ArrayList.
 
Yes, you're right. I overlooked that the [] was missing and now I see what you were trying to do. And no, that wouldn't work. I'm surprised that it even compiled.

Although I'm still not sure why you saw Array to be advantageous over Object[] or ArrayList.

Arrays of arrays can also be done as:
Code:
Object[][] arrays = new Object[x][y];
or
Code:
Object[,] arrays = new Object[x,y];
Keep in mind this can be expanded beyond 2 dimensions to an arbitrary number of dimensions.
 
No advantages with using Arrays, I just though it could work and it bothered me that it did not and compiled. It was like an itch I just could not scratch.

I still prefer Object[], but I now I know how to create an array using Array if ever need to.
 
I don't think I've ever seen Array used like that. Typically, you use the Array methods to operate on existing arrays of whatever type you specified. It's an abstract class that is the base class for all arrays.

I don't know how this even worked considering you can't instantiate an abstract class.

If you want a functional array, you would have to create array(s) of a type or of an object.
 
Back
Top