0

My ctor return an object which is multidimentional array. Depends on constructor argument, the object returned by ctor can be different rank array, but always int.

object arr = new int[2,2,2];

or

object arr = new int[2,2,2,2,2];

or

object arr = new int[0,0];

Having arr object constructed, and knowing what it is (GetType()), I'd like to access array methods like Rank, GetLength, GetValue etc. How I can access child specific methods from the object level? For now I have only four methods for arr object accessible: Equals, GetHashCode, GetType, and ToString

3
  • 2
    Can I ask why you have it as a type object in the first place when you know its an integer array? (or similar at least) Commented Aug 5, 2015 at 9:35
  • 2
    You can type it as Array but still that's not a right thing to do. Type it as int[]. Your underlying problem is different. Maybe you need different implementation of the classes for different types of array. Commented Aug 5, 2015 at 9:38
  • @Sayse: yes I could store it as Array not as an object. Valid point. Commented Aug 5, 2015 at 9:41

3 Answers 3

1

Cast the object into an array, like this:

((int[])arr).Rank
((int[])arr).GetLength()

or

(arr as int[]).Rank
Sign up to request clarification or add additional context in comments.

1 Comment

believe me or not, but I was just about to answer my question myself. My answer would be the same as yours. Thanks
1

You could just declare your variable as Array:

Array arr = new int[2,2,2,2,2];
int rank = arr.Rank;

Or cast to Array:

object arr = new int[2,2,2,2,2];
Array array = (Array)arr;

Comments

0

You have to cast it back to an Array object and then these methods will be available!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.