Array Size (Length) in C#

C# What integer value of array.length

Yes, you are correct. According to the MSDN docs, an array's .Length property returns the total number of elements in all the dimensions of the array and zero if there are no elements in the array.

The .Length property is an integer, so there's no need for any conversion. Though there's really no need to assign it because it is a constant-time operation, you could assign it like this:

int arrayLength = array.Length;

Array.Length on MSDN

How do you get the width and height of a multi-dimensional array?

You use Array.GetLength with the index of the dimension you wish to retrieve.

change array size

No, try using a strongly typed List instead.

For example:

Instead of using

int[] myArray = new int[2];
myArray[0] = 1;
myArray[1] = 2;

You could do this:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);

Lists use arrays to store the data so you get the speed benefit of arrays with the convenience of a LinkedList by being able to add and remove items without worrying about having to manually change its size.

This doesn't mean an array's size (in this instance, a List) isn't changed though - hence the emphasis on the word manually.

As soon as your array hits its predefined size, the JIT will allocate a new array on the heap that is twice the size and copy your existing array across.

Determine array size after GetType()

There is no information on the type regarding the size of the array. It is just an array, nothing more.

So the only way to get the size is going back to the instance. There, and only there, you can find the size of the array.

You could do that by querying the Length property using reflection.

Element Size of a C# Array?

This is the best way I've found so far:

int elementSize = System.Runtime.InteropServices.Marshal.SizeOf(pixels.GetValue(0));

Array of an unknown length in C#

Arrays must be assigned a length. To allow for any number of elements, use the List class.

For example:

List<int> myInts = new List<int>();
myInts.Add(5);
myInts.Add(10);
myInts.Add(11);
myInts.Count // = 3

C# user input to set size of array and loop array to display elements

Because you create a new array inside of your loop that is the size of the "score" the user entered and then you loop over it. The values are all zero because when you create an array it is populated with the default value of the type, in this case 0. The second loop should be after the first one and you shouldn't be creating arrays inside of the first loop, just populating the original array (score) that you created to begin with.

Here's what you actually want. Note that you should index starting at 0 and not 1.

Write("How many test scores total: ");
string sSize = ReadLine();
int i = Convert.ToInt32(sSize);
int[] score = new int[i];

// create the loop of asking the test scores limited to the array sSize
for (int a = 0; a < i; a++)
{
Write("Please enter a test score " + (a + 1) + " from 0 to 100: ");
string testArray = ReadLine();
int g = Convert.ToInt32(testArray);
score[a] = g;
}

//create loop to display all test scores
foreach (var item in score)
Console.WriteLine(item);

You may also want to consider using int.TryParse so you can determine if the user enters an invalid value.

What is the Maximum Size that an Array can hold?

System.Int32.MaxValue

Assuming you mean System.Array, ie. any normally defined array (int[], etc). This is the maximum number of values the array can hold. The size of each value is only limited by the amount of memory or virtual memory available to hold them.

This limit is enforced because System.Array uses an Int32 as it's indexer, hence only valid values for an Int32 can be used. On top of this, only positive values (ie, >= 0) may be used. This means the absolute maximum upper bound on the size of an array is the absolute maximum upper bound on values for an Int32, which is available in Int32.MaxValue and is equivalent to 2^31, or roughly 2 billion.

On a completely different note, if you're worrying about this, it's likely you're using alot of data, either correctly or incorrectly. In this case, I'd look into using a List<T> instead of an array, so that you are only using as much memory as needed. Infact, I'd recommend using a List<T> or another of the generic collection types all the time. This means that only as much memory as you are actually using will be allocated, but you can use it like you would a normal array.

The other collection of note is Dictionary<int, T> which you can use like a normal array too, but will only be populated sparsely. For instance, in the following code, only one element will be created, instead of the 1000 that an array would create:

Dictionary<int, string> foo = new Dictionary<int, string>();
foo[1000] = "Hello world!";
Console.WriteLine(foo[1000]);

Using Dictionary also lets you control the type of the indexer, and allows you to use negative values. For the absolute maximal sized sparse array you could use a Dictionary<ulong, T>, which will provide more potential elements than you could possible think about.



Related Topics



Leave a reply



Submit