Sign in to follow this  

C# questions

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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][i]. I personally stick with [i][i] (jagged array I believe?) as it's more intuitive.

Share this post


Link to post
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 Drakkcon
I'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?

Short answer:
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 specified
int[,] rectArray1 = new int[3,4]; // 3x4 array
int[,] rectArray2 = new int[5,6]; // 5x6 array

// initializing upon declaration: first 5 prime pairs
int[,] 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 dimension
int[,,] rectArray3D = new int[13,5,8]; // 13x5x8 array
int[,,,] rectArray4D = new int[13,4,5,8]; // 13x4x5x8 array





Jagged arrays:

// declaring a two-dimensional jagged array
int[][] jaggedArray1 = new int[3][];

// initializing upon declaration; size of first dimension is implied, don't need to specify
int[][] 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 dimension
int[][][] jaggedArray3D = new int[28][][]; // 3-dimensional jagged array
int[][][][] jaggedArray4D = new int[19][][][]; // 4-dimensional jagged array





Long answer:
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?

Short answer:
More or less, yes.

Long answer:
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this