codeasy .net
or Sign Up
C# Beginner > 2 Victory is close > Introduction to arrays in C#

Under the bridge

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 shit? 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

Create an array of 5 integers. Don't fill the array with any values. Output the default values of every cell to the console, each on a new line.

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. At 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]);
}

Read an integer count from the console. Then create an array of integers of size count. The value of every cell should be its index value. Output every cell of the array to the screen, each from the new line, starting from the cell with index 0. For example:
>4
0
1
2
3

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

Call a method GetNumbersFromConsole that reads a count value of double, and then reads count values from the console into a double array. Create another array and copy to it all the values from the first one. Finally, output every value in the array to the console, each on a new line. For example:
>3
>1.12
>-2.333
>399.76
1.12
-2.333
399.76

Call a method GetNumbersFromConsole that reads a count value of double, and then reads count values from the console into a double array. Find the maximum of the values in the array and print "Max is: {number}" to the console. For example:
>3
>1.12
>-2.333
>399.76
Max is: 399.76

Call a method GetNumbersFromConsole that reads a count value of int, and then reads count values from the console into an int array. Print all values from the array on one line, in reverse order, each separated by a space. For the purposes of this task, you should NOT have a space at the end of the line. For example:
>3
>1
>2
>3
3 2 1
/* NOTE: Do not use Console.Write("\b") to delete the last character. It is not supported by the code runner. */

Call a method GetNumbersFromConsole that reads a count value of int, and then reads count values from the console into an int array. Calculate and print the sum of all elements of this array in a format "Sum: {number}". For example:
>3
>1
>3
>2
Sum: 6

Call a method GetNumbersFromConsole that reads a count value of int, and then reads count values from the console into an int array. Separate 'Big' and 'Little' integers into two separate arrays: Big if the integer is greater than 100; Little otherwise. Then output the values in the Big array on one line and the values in the Little array on the next in the following format: "Big: {numbers}" and "Little: {numbers}". Separate numbers on each line by a space, but do NOT include space at the end of the lines. For example:
>4
>101
>756
>3
>255
Big: 101 756 255
Little: 3

Arrays: one more joke

C# Beginner > 2 Victory is close > Introduction to arrays in C#

This site uses cookies. By continuing to browse this site you are agreeing to our use of cookies.

Got it!