Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a variable of type object, which I know to contain an array. I know nothing about the contained type (it can vary), and neither I need to. All I want is to get the array size/length (number of elements in that array).

What is the right way of doing it in C#?
Posted

You can cast your object to Array
C#
object o = new int[3];
string test = (o as Array).Length.ToString();
MessageBox.Show(test);


or if it is a list:
C#
object o = new List<int>(3);
string test = (o as IList).Count.ToString();
MessageBox.Show(test)

and you can combine both using the operator is
C#
if(o is IList) size = (o as IList).Count;
if(o is Array) size = (o as Array).Length;


Update:
After test, int[] is IList too, but the List<int> is not Array, you may use the IList in all case
 
Share this answer
 
v2
Comments
Vitaly Tomilov 20-Dec-12 8:58am    
Thank you. I can confirm that methods 1 and 2 work fine. Method 3 is not applicable, as I said I do not know the type, and do not care either. I wonder though, how the first two methods compare to the method I suggested with use of keyword dynamic... ;) I'm not sure what really happens underneath it :)
Hi,

You can try this:
C#
int lengthOfArray = ((Array)obj).Length; // change obj into the name of your object

Hope this helps.
 
Share this answer
 
v2
Comments
Vitaly Tomilov 20-Dec-12 9:17am    
Good fix on your part, because the first version, suggesting use of (object[]obj).Length was no good, but use of Array works :)
Thomas Daniels 20-Dec-12 9:21am    
Thank you!
its impossible to get the length without know the object is of type Array. You can check whether the object instance is of type array using 'is' operator and conver it using 'as' operator.

/Edit.
try casting the Object with ICollection and use the Count Property.
 
Share this answer
 
v2
Comments
Vitaly Tomilov 20-Dec-12 8:47am    
I said in the question that I already know it is an array.
Jibesh 20-Dec-12 8:48am    
oh.. my bad.. sorry.
I've just discovered that I can do the following trick that became possible only in .NET 4.0, using keyword dynamic:
C#
object obj; // We know it is an array, don't care about which type;
int arraySize = ((dynamic)obj).Length;

This seems to work fine. I wonder though, if there is any caveat to doing it... :)
 
Share this answer
 
v2
Comments
Jibesh 20-Dec-12 9:00am    
you can also use ICollection base.
lewax00 20-Dec-12 10:20am    
If you're using it a lot, using dynamic might cause a performance hit (I'm pretty sure it uses reflection, which from what I've heard can be a little slow), otherwise it should be fine (and should even work if you later pass in a custom class, as long as it has an integer property called Length).
C#
((ICollection)myObject).Count


(error handling omitted).
 
Share this answer
 
v2
Comments
Thomas Daniels 20-Dec-12 9:25am    
What you can add: The ICollection class in in the System.Collections namespace.

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