Little confused about declaring an array

357mag

Well-known member
Joined
Mar 31, 2023
Messages
58
Programming Experience
3-5
My books usually show that if you want to use an array in C# you gotta do something like this:

int [ , ] rentAmount = new int [4, 4];

But I was looking at a different one of my books and the author wrote out a program she did this instead:

int [ , ] rentAmount = { { 400, 450, 510... and on and so forth.

She did not use the new int and the brackets either, and the program runs fine.

I was just wondering, and it looks like you don't need to include the new keyword followed by int and the brackets?
 
Firstly, you should understand what "declare" actually means. People are a bit loose with their language and it can confuse beginners, even if they don't realise it. You don't declare an array. You declare a variable. You then create an array and assign it to the variable. You're talking about creating an array, not declaring an array or an array variable.

When you create an array object, you are required to specify the size. You cannot change the size of the array once it is created, so you obviously have to know that size when you create it. Any code that appears to resize an array doesn't actually do so. For instance, the Array.Resize method doesn't actually resize and existing array. It creates a new array of the specified size and assigns that to the specified variable. The old array object still exists and, in fact, any other variables that referred to it will still do so.

There are different ways to specify the size of an array. Your first code snippet:
C#:
int [ , ] rentAmount = new int [4, 4];
creates an array of the specified size where all elements have their default values. Your second code snippet creates an array from the specified values, so the size of the array is implicit based on the number of values provided. When you use that second option, it used to be the case that you had to use the new keyword but a recent version of C# (can't recall which exactly, but you can look it up if you want) dropped that requirement. You can still do this:
C#:
int[] arr = new int[] {1, 2, 3};
or this:
C#:
int[] arr = new[] {1, 2, 3};
but this is allowed too:
C#:
int[] arr = {1, 2, 3};
It's a case of making code simpler by stripping out the noise when the intent is still obvious and unambiguous.

Just note that, if you want to use var, you do still need to use the new keyword:
C#:
var arr = new[] {1, 2, 3};
 
I was just wondering, and it looks like you don't need to include the new keyword followed by int and the brackets?
True, if you’re specifying the values then the compiler can work out how big your array is
She did not use the new int and the brackets either
But you would have to if you didn’t know your values at the time you wrote the program
 
My books usually show that if you want to use an array in C# you gotta do something like this:

int [ , ] rentAmount = new int [4, 4];

But I was looking at a different one of my books and the author wrote out a program she did this instead:

int [ , ] rentAmount = { { 400, 450, 510... and on and so forth.

She did not use the new int and the brackets either, and the program runs fine.
gta 5 download for android offline
I was just wondering, and it looks like you don't need to include the new keyword followed by int and the brackets?

Yes, you can initialize a two-dimensional array in C# without using the new keyword by directly providing its initial values using the curly brace syntax. The syntax you mentioned is called an array initializer, which is a shorthand way of declaring and initializing an array in a single statement.

In the example you provided, the rentAmount array is being initialized with its initial values enclosed in curly braces. This approach is useful when you know the initial values of the array elements at compile-time. However, if you don't know the initial values at compile-time, you need to use the new keyword followed by the array size to create an empty array, and then assign values to its elements.
 
Yes, there are multiple ways to create and initialize arrays in C#. The first approach uses the new keyword followed by the array type and dimensions in square brackets, while the second approach initializes the array with values using curly braces and nested arrays. Both approaches are valid and will work, and you can choose the one that is more readable and appropriate for your specific use case.
Thanks
 

Latest posts

Back
Top Bottom