Intermediate2 Victory is closeIntroduction to arrays in C#

We Are All the Same

The day when I could travel back to my own time was getting closer and closer. Until that time came, I resolved to learn as much as I could from this desperate future. I decided to try to find the shady guy from the C gang once again. Now that I had a better understanding of programming in general, I had a feeling that he still had some knowledge that would help broaden my education.

I found him close to the old bridge, coding something on his laptop. This was turning into a familiar sight - everyone in this world was always coding something or another.

I started the conversation this time. "Hi, do you remember me?"
"Yo, of course! You're that newbie from the resistance base. How is your code going? New bug every day, kid?" he said with a laugh.

"Actually, I've learned a lot in the last month." I said. "I know about console input, for and while loops, .Net framework structure... And a lot of other things. I'm not so much of a newbie anymore!"

".Net frame-what? What is this crap? You know, man, I don't care. You need to be careful with forbidden knowledge, kid, machines can punish you if they think you're in too deep."
"No worries about the machines," I said proudly. "I've made a friend among them!"

"What did you say?!" he replied incredulously, closing his laptop and focusing all of his attention on me.
"Did you say you have a friend among machines? Are you crazy? How are you still alive, man? Maybe the better question is why - are you on their side now? Stay away, kid! I don't want trouble."

"Whoa, easy now!" I replied, realizing that I might have divulged too much. Oh well; I was in deep enough that I figured I may as well tell this stranger the full story. "This machine is broken, and it loves humanity. It was disconnected from the GMN (Global Machine Network) and moved into the base. It is not a dangerous machine."

"It might be lying to you," the man said, "I've heard of machines that pretend to be disconnected from their network, man. Did you check it? There's an array of numbers that you can give to a machine and ask it to do some math on those numbers. If it does, you can trust that machine. This test was developed back in 2085 at the beginning of the war by a clever woman named Lillian. There's some sort of hidden logic that prevents evil machines from passing this test. To apply the test you need to know arrays. Do you?"

"Not at all." I answered. The discussion thus far had been fairly confrontational, but it looked like my plan to learn something new was still working.
"Yo, alright! Listen up then, man!"

c# arrays joke

Arrays

"When you were a kid in school, did your teachers have a list of all the students in the class along with their grades?" the guy asked.
"Yes" I answered.
"Yes, man! Then we're on the same page. In programming, you use an array data structure to store multiple variables of the same type. To create an array of integers, you use the new keyword and specify the type, name, and size of the array ."

int[] studentMarks = new int[10];

"It also works if the array size is a variable." he added.

int studentsCount = 10;
int[] studentMarks = new int[studentsCount];

"Is that a list of 10 integers?" I asked, pointing to his laptop.
"Exactly! In memory, it would look similar to this." he said, drawing a quick doodle.

c# array declaration

"Creating this array is the same as creating 10 int variables." the guy explained.
"Hm... interesting." I said, taking it in. " To create an array I need to declare a type of element, then add [] , then specify a name, and assign to it a new array with the size inside. Quite a few things to remember."

"One more thing, kid. After you create an array, it's initially filled with a default value of its type. The default value for int is 0, which is why the array I showed you was filled with zeros ."

"Got it." I said. "But how do I fill an array with values other than 0?"

To access any cell of the array, you need to specify the cell's index. Indexing of arrays in C# starts with 0 and increases by 1 for each next cell. For example, this code creates an array of 10 numbers, and then assigns 99 to the first array cell:

int[] studentMarks = new int[10];
studentMarks[0] = 99;

In memory, this array looks like this:

c# array declaration

"Do you follow, man?" the stranger said, adding condescendingly, "Or don't they teach you such complicated things in your resistance base?"
"I follow just fine." I said. "Continue."

The following code fills all the cells of the array with different values. The subsequent array in memory is shown below.

int[] studentMarks = new int[10];
studentMarks[0] = 99;
studentMarks[1] = 63;
studentMarks[2] = 54;
studentMarks[3] = 12;
studentMarks[4] = 33;
studentMarks[5] = 88;
studentMarks[6] = 72;
studentMarks[7] = 67;
studentMarks[8] = 94;
studentMarks[9] = 5;

c# array declaration

There is also a quicker way to create an array if you know all the values at the time of its creation. The following line of code does exactly the same as the code in the previous example:

int[] studentMarks = new int[10] { 99, 63, 54, 12, 33, 88, 72, 67, 94, 5 };

"Wow, that is much quicker!" I said.
"Yes," the man replied, "I like this one more too."
"Can you show me how to read values from the array?" I asked.
"No problem, man! To read values from the array, you must specify the index of the cell you want to read after the array name, the same as you did when writing values to the array ."

int[] studentMarks = new int[10] { 99, 63, 54, 12, 33, 88, 72, 67, 94, 5 };
Console.WriteLine(studentMarks[1]);
// Outputs: 63
int[] studentMarks = new int[10] { 99, 63, 54, 12, 33, 88, 72, 67, 94, 5 };
int copyOfSecondElement = studentMarks[1];
Console.WriteLine(copyOfSecondElement);
// Outputs: 63

Arrays: one more joke

"Cool kids usually use loops with arrays." the man said, now starting to show off. "Look here - I've written a program that reads the number of students from the console, then creates an array of student's grades and pre-fills it with 10, because each student begins with 10 points. In the end, the program prints the points of every student," he said, showing me the screen of his laptop.

int studentsCount = int.Parse(Console.ReadLine());
int[] studentMarks = new int[studentsCount];

for(int i = 0; i < studentsCount; ++i)
{
    studentMarks[i] = 10;
}

for(int i = 0; i < studentsCount; ++i)
{
    Console.WriteLine(studentMarks[i]);
}

Good job! You can also create arrays of different types and work with them the same as you do with int arrays, like this:

double[] myDoubleArray = new double[5];
myDoubleArray[2] = 2.15;

float[] myfloatArray = new float[5];
myfloatArray[2] = 2.15f;

string[] myStringArray = new string[5];
myStringArray[3] = "Hello!";

char[] myCharArray = new char[5];
myCharArray[1] = 'a';

To get the length of an array, you can read its Length property. For example, if you've created an array myArray of length 5, then myArray.Length returns a value of 5. This can be helpful if someone else creates an array of data, which you then bring into your program, but don't know what the size of the array was when it was created.
"I need to go, see ya, man!" the stranger said, suddenly. "Solve all these tasks and take care, maybe we'll meet again!

double[] myArray = new double[5];
Console.WriteLine(myArray.Length);
// Outputs: 5