In addition to working with the strings and the lists that we mentioned above, it is time for us to take a look at the C# arrays and how this works.
With the array, we can make a group of variables and store them all, as long as these variables are the same data type.
You will be able to declare the array that you would like to work with by specifying the type of its elements.
So, if you would like the array to store one of the elements, regardless of the element that we bring up, we would need to specify the object as its type.
Before we take the time to write out a bit of the code that we need to help us create some of the arrays that we want, it is important for us to go a bit more into what an array is and why this is something that we want to work with overall.
An array is going to come with a number of properties that we are able to use, and some of these will include:
- When we use an array, it is possible that it will come as either single-dimensional, multidimensional, or jagged.
- The dimensions and the length that you are going to see with these dimensions in our array are going to be established when you first create the array instance.
These are going to be values that we will not be able to change once we create it, no matter how long we decide to use the instance overall.
- The default values that we are able to see with the elements of an array that is considered numerical are going to start with zero, and then when we work with the elements that are part of the reference, these are going to be null.
- When we are working with an array that is seen as jagged, we need to remember that this is actually an array of arrays.
This means that all of the different elements that we are going to use are like the reference types, and they will be initialized when we come to null.
- We also have to remember that the arrays are going to be indexed at zero.
What this is going to mean for our coding is that the array is going to start from 0 as the first element, and then the last element will be n-1.
- The different elements that we are able to find in our array are able to come in as any type that we would like based on our code.
They can even come in as an array type.
- The types of arrays that we are working with here are going to be a reference type that we will be able to derive from the abstract base type known as Array here.
Now, you will find that there are quite a few times when you work on some of the codes you want to do in C# when you will need to bring in these arrays.
And having a good idea of how to work with them, and what they can do for the codes you want to write will be important.
That is why it is so important to understand some of the features that we listed above, and then we are able to explore some of the different parts of the code that we can do with these arrays below.
There are a number of methods that we are able to use when it comes to creating our own arrays in the C# language.
The code that we are going to look at below is helpful because it is set up to help us work with several different arrays, including the jagged array, the multidimensional array, and the single-dimensional array based on the codes that we are working with.
The code that we can do with this one will include the following
class
TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array.
int
[] array1 =
new
int
[
5
];
// Declare and set array element values.
int
[] array2 =
new
int
[] {
1
,
3
,
5
,
7
,
9
};
// Alternative syntax.
int
[] array3 = {
1
,
2
,
3
,
4
,
5
,
6
};
// Declare a two dimensional array.
int
[,] multiDimensionalArray1 =
new
int
[
2
,
3
];
// Declare and set array element values.
int
[,] multiDimensionalArray2 = { {
1
,
2
,
3
}, {
4
,
5
,
6
} };
// Declare a jagged array.
int
[][] jaggedArray =
new
int
[
6
][];
// Set the values of the first array in the jagged array structure.
jaggedArray[
0
] =
new
int
[
4
] {
1
,
2
,
3
,
4
};
}
}
The strings, lists, and arrays in the C# language can add on a new level to some of the work that we are trying to accomplish, and it is important for us to take the time to learn how they can be added to our codes.
We can see that each one is going to be a little bit different, and it often depends on what we are hoping to get out of the process and how we want the code to behave as well.
Take some time to look at these three options and learn how they work together, how they are different, and how we are able to use them in our own codes.