Click here to Skip to main content
15,898,752 members
Please Sign up or sign in to vote.
1.82/5 (3 votes)
See more:
Hello All,

I've seen a declaration in a class like below:

C#
public class MyClass : IDisposable
{
   double[,] MyDouble;

   public MyClass()
   {

   }

   public void Dispose()
   {
   }
}


Two quick questions. In the absence of private or public, it's private right? And what does the [,] mean? Is that an open ended array?
Posted

[,] is a Multidimensional array as described here : [^]
and yes private as found here [^]
 
Share this answer
 
v2
No. Arrays are never open-ended; their dimensions are even immutable (please see below).

It simply means that the dimensions of the array are not known at the point of the array variable declaration, but then the array object is never created. When you create an array object itself using new, you always pass dimensions to it, and since that the array dimensions cannot be modified.

Why using an unknown dimensions then? Oh, this is very important.

This is the abstract interface to a variable. [EDIT] First of all, the variable declaration declared the array rank (number of indices to be use, 2 in case of [,], 3 for [,,], etc.); I'm adding this explanation thanks to the good comment by Matt T Heffron (see comments below); thank you, Matt. [END EDIT]. The dimensions can be variables. For example, you create an array in a method, so, in one call, you create the actual object with one set of dimensions, in other call, with different dimensions. When you use the variable referencing already created array object, you inquire about actual dimensions from the array object itself:
http://msdn.microsoft.com/en-us/library/system.array.length.aspx[^],
and, in case of rank > 1: http://msdn.microsoft.com/en-us/library/system.array.getlowerbound.aspx[^],
http://msdn.microsoft.com/en-us/library/system.array.getupperbound.aspx[^].

And, be careful with System.Array.Resize: the array is never actually resized: in fact, a brand-new array object is created, available data is copied from "old" array object to "new" one and returned. If you overwrite the variable with this brand-new array, "old" array object will eventually be garbage-collected.

[EDIT]

For open-ended data structures, you can use collections:
http://msdn.microsoft.com/en-us/library/system.collections.aspx[^],
mostly http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

Basically, that's all you need to know.

—SA
 
Share this answer
 
v4
Comments
ridoy 21-Mar-13 14:47pm    
good explanation..+5
Sergey Alexandrovich Kryukov 21-Mar-13 14:48pm    
Thank you very much,
—SA
Matt T Heffron 21-Mar-13 15:08pm    
You didn't mention that the dimensionality (rank) of the array IS specified at the declaration.
Otherwise, good job.
Sergey Alexandrovich Kryukov 21-Mar-13 15:11pm    
I just assumed it's clear, but maybe this is a good idea to explain.
Thank you, Matt.
—SA
Sergey Alexandrovich Kryukov 21-Mar-13 15:15pm    
I added some explanation by example, hope it's clear, your comment is of course credited.
Thank you again.
—SA

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