Public Group

# C# questions

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm learning C#, and I'm very impressed so far. As a C++ programmer, I have two questions though: What's the difference between a multi-dimensional array and a jagged array? And... I can't understand a delegate, is it just a function pointer? Thanks!

##### Share on other sites
I've just started reading about C# as well, so maybe I can help.

A multidimensional array has the same cardinality within a dimension; a jagged array relaxes that restriction.

For example, in 2-dimensions:
Array : 10 rows, each row has exactly 8 elements
Jagged array : 10 rows, which have 1, 2, ..., 10 elements respectively.

Think of a jagged array like a std::vector<std::vector<something> > - not every element of the first dimension needs to have the same number of elements.

A delegate, from what I've read so far, is indeed identical to a function pointer (or pointer to member function) in C/C++. However, you can bind more than one to a function call, using + and -, meaning that you can call more than one delegate with one call (once you've bound them).

HTH,
Jim.

##### Share on other sites
The interesting part of the delegate is that it binds not just a function pointer, but also an object together. Calling a delegate is like calling that member function on a specific instance of a class.

##### Share on other sites
A delegate is a type safe function pointer with much more functionality. However, its performance is quite lacking and is indeed quite slow. There are some articles on msdn.microsoft.com that have benchmark results. I suggest searching through and reading them.

Not sure what the difference between the two arrays are other than one is used with [i, i] and the other is . I personally stick with (jagged array I believe?) as it's more intuitive.

##### Share on other sites
Thanks, those are good explanations! I think using delegates has a much better syntax than function pointers did.

##### Share on other sites
The explanations above were mostly good, but I'll just clarify a few things here -- some information above was incorrect. Take a deep breath, 'cuz I tend to be a bit long-winded. ;)

Quote:
 Original post by DrakkconI'm learning C#, and I'm very impressed so far.

I'm glad you enjoy it too. =)

Quote:
 What's the difference between a multi-dimensional array and a jagged array?

In a multi-dimensional array (called a rectangular array), each sub-array must be identically sized to the others. In a jagged array, each sub-array can be differently sized, and you need only declare the size of the first dimension. Some examples of declaring both kinds are shown below.

Rectangular arrays:
// declaring a two-dimensional rectangular array -- all dimensions must be specifiedint[,] rectArray1 = new int[3,4];      // 3x4 arrayint[,] rectArray2 = new int[5,6];      // 5x6 array// initializing upon declaration: first 5 prime pairsint[,] primePairs = new int[5,2]                    {                      // each of the 5 sub-arrays has the same number of elements                      {2, 3},    // 2 elements                      {5, 7},    // 2 elements                      {13, 17},  // 2 elements                      {19, 23},  // 2 elements                      {29, 31}   // 2 elements                    };// each "," indicates another dimensionint[,,] rectArray3D = new int[13,5,8];       // 13x5x8 arrayint[,,,] rectArray4D = new int[13,4,5,8];    // 13x4x5x8 array

Jagged arrays:
// declaring a two-dimensional jagged arrayint[][] jaggedArray1 = new int[3][];// initializing upon declaration; size of first dimension is implied, don't need to specifyint[][] jaggedArray2 = new int[][]                      {                        // each of the sub-arrays has a different size                        new int[] {5,24,1,3},  // 4 elements                        new int[] {498,2},     // 3 elements                        new int[] {24},        // 1 element                      };// each [] pair corresponds to another dimensionint[][][] jaggedArray3D = new int[28][][];     // 3-dimensional jagged arrayint[][][][] jaggedArray4D = new int[19][][][]; // 4-dimensional jagged array

All arrays in C# are either rectangular or jagged. A rectangular array is an array of n dimensions of sizes s1 x ... x sn. Every array of n dimensions and whose first dimension's size is s1 can be considered as a series of s1 (n-1)-dimensional arrays. Put another way, declaration of a rectangular array makes an n-dimensional "box" with dimensions s1 x ... x sn.

For instance, suppose you have the declaration int[,,] myArray = new int[6,5,9];. This is a 3-dimensional rectangular array of size 6 x 5 x 9. You could also consider it as a series of 6 arrays of size 5x9; each of these, in turn, could be considered as a series of 5 arrays of size 9.

With jagged arrays, this isn't the case. Instead, you have an array of arrays; and each of these arrays has its own size. For instance, suppose you have the declaration int[][][] myArray = new int[4][][];. This is a 3-dimensional jagged array containing four 2-dimensional arrays.

It is also possible to mix jagged and multidimensional arrays. The declaration int[][,] multiArray1 = new int[9][3,4]; is a 1-dimensional jagged array of size 9 whose elements are 2-dimensional rectangular arrays each of size 3x4. The declaration int[,][] multiArray2 = new int[4,6][]; is a two-dimensional rectangular array whose elements are one-dimensional jagged arrays of varying size.

Quote:
 I can't understand a delegate, is it just a function pointer?

More or less, yes.

A delegate is a reference type that refers to any method whose signature matches its declaration. Like the declarations of other types, delegate declarations typically appear inside a namespace or inside a class, but not inside a method definition. Delegates also serve as the basis for events, another important part of C#. Here's an example, with commentary:

// delegate's type is any method with the signature "void f(int)"delegate void IntegerHandler(int i);class Program{   public static void Main()   {      // delegate instance created and passed to function Invoker      Invoker(new IntegerHandler(IntPrinter));   }   // Invoker takes a delegate of type IntegerHandler and calls it with parameter 21   public static void Invoker(IntegerHandler handler)   {      handler(21);   }      // IntPrinter is method that could be assigned to an IntegerHandler instance   public static void IntPrinter(int i)   {      System.Console.WriteLine("Called by delegate with number: {0}.", i);   }}

HTH,

##### Share on other sites
To give a little unneeded info ...

A Jagged array is plain an simple an array of arrays (of arrays ...)

just like this C++ example:

typdef std::deque<float> DequeOfFloats;
typdef std::deque<DequeOfFloats> SquareDequeOfFloats;
typdef std::deque<CubicDequeOfFloats> CubicDequeOfFloats;

say you have an item CubicDequeOfFloatsmyJaggedCube

when index it: myJaggedCube[2], what it returns is a SquareDequeOfFloats, and if you index that (aka myJaggedCube[2][4]) you have a DequeOfFloats, and if you index that (myJaggedCube[2][4][2]) you have a float. So you see a Jagged array returns an intermediate type after each [] operator ... which is then indexed by the next operator.

A multidimentinal array in C# is not like that at all. It has an interface which takes multiple indexes in the simple function call ... and always returns an item of the type it holds.

So: float myCube[5,5,5]

can only be indexed with all 3 numbers at once ... basically it has a [] operator which takes 3 ints instead of 1.

myCube[3] - doesn't work
myCube[2,4,2] --> yeilds a float

##### Share on other sites
Thanks for the detailed explanations. Looking at it now, I probably won't use Jagged Arrays much because most of the things I use arrays for are game maps. I also want to note that the syntax for declaring multi-dimensional arrays on the heap is far better than C++'s. Thanks for the help everyone!

##### Share on other sites
I agree, but realize C doesn't have multidimensional NON-Jagged arrays :)

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×