   Jagged Arrays in C#
  Hi, guys.Basically, I need to traverse every element in a jagged array, but I don't know the number of dimensions in the array. For example, it could be object[][][][] or object[][], I just don't know. Also, I can't use foreach because that will return another array. For example, I can doobject[][][] objects = ...foreach(object[][] obj in objects)What I'd really like to do isobject[][][] objects = ...foreach(object obj in objects)As you can see, my problem is that object[][][] is really a one-dimensional array of object[][], not a multidimensional array of object. How can I traverse every object in object[][][] (keeping in mind that there could be any number of brackets in the array).Thanks in advance.   
  I'm not sure how to tell the number of dimensions. I just wanted to post to know what in the universe you would possibly want to do this to accomplish? Just to learn a nifty trick, or do you have some kind of real world use for this in mind?   ;  I'm using this with reflection. Basically, I'm trying to write a class that saves an instance of a class to a file. However, when an array is encountered, I want to save each object in it individually instead of saving the array as a whole. I know it sounds overcomplicated, but I DO have good reasons for this.Oh, and I can figure out the number of []'s, I just can't hardcode that number into my code. Basically, my code has to be able to work with object[], object[][][], etc.   ;  You want the .Rank property of the Array class. IE:object[][][] objects;Console.WriteLine( objects.Rank ) ; // outputs 3;  That's what I'm saying - it doesn't. object[,,] objects;Console.WriteLine(objects.Rank);outputs 3. What you wrote will output 1 because it's a one-dimensional array of object[][]. When you use commas, it is indeed a 3-dimensional array of object. I will actually use the comma version in my game, but I want my saving engine to be able to handle jagged (multiple []'s) arrays, just in case.   ;void traverseArray(object[] array){    foreach(object element in array)    {   object[] element_array = element as object[];   if (element_array != null)   {  // the element is an array, so traverse it  traverseArray(element_array);   }    }};  If you want to save an instance of a class why don't you just to this:class CClass{...} ClassInstance;#define WriteClass(_class, _file) fwrite(&_class, sizeof(_class), 1, _file)FILE *pFile = fopen(...);WriteClass(ClassInstance, pFile);;Quote:Original post by YohomythIf you want to save an instance of a class why don't you just to this:*** Source Snippet Removed ***I'm using C#, not C++. And the reason I want to use custom serialization is for versioning purposes.   ;  Oh, and Tommy, this is exactly what I needed. Thanks, and enjoy your rating!   ;  Won't .NET's serialization take care of this for you?   