+ 1
Guys anyone can explain about Array?
about Array
3 Answers
+ 2
Imagine that you have a group of items that need to be declared. For eg. brands of cars.
Now, normally, to declare them, you would have to go about like this.
string toyota = âToyotaâ;
string ford = âFordâ;
string hyundai = âHyundaiâ;
Instead, you can do this
string[] carNames = new string[]{âToyotaâ, âFordâ, âHyundaiâ};
By doing this, a common group of data is declared into one variable name.
To access it, you have to go about like this.
Console.WriteLine(carNames[0]);
Which will display Toyota
This is because the first item stored in the array is placed in storage point 0.
To display the next item, you just have to do
Console.WriteLine(carNames[1]);
To elaborate:
carNames[0] = Toyota
carNames[1] = Ford
carNames[2] = Hyundai
Same principle applies for numbers as well.
Instead of declaring
int a = 25;
int b = 30;
int c = 35;
You can do
int[] num = new num[]{25, 30, 35};
And can call them by using:
Console.WriteLine(num[0]); //displays 25
Console.WriteLine(num[1]); //displays 30
I hope this clarifies a bit about arrays in C#.
0
An ordered list of objects that are grouped together in memory. They are accessed by a number between 0 and arrayâs length - 1, 0 being the first one, 1 second ect. Java example:
Note: Java like most languages uses [num] to access a element inside an array which is called an index
int[] arr = {1, 2, 3};
System.out.println(arr[0] + arr[2]); // 1 + 3
System.out.println(arr[1]); // 2
0
tnx guys