[“C#”, “Arrays”, “Lesson”, “part”, “1”]

Advertisements

Hello everybody,

Michael here, and this post will be a part 1 of my C# array exploration, covering 1-dimensional arrays!

You could say this array moves in 1, 2 step.

What are arrays? Well, you’ve likely seen me cover them in Java and Python-arrays basically store lists of values in your code. The values could be of any type-strings, integers, etc.

For a refresher, here’s what arrays look like in Python:

pythonArray = ['test1', 'test2', 'test3', 'test4', 'test5']

And here’s what arrays look like in Java:

String[] javaArray = {'test1', 'test2', 'test3', 'test4', 'test5'};

[“C#”, “1-dimensional”, “arrays”]

Now, the first thing I will show you how to do is make a simple 1-dimensional array in C#. Take a look at this example:

string[] languages = ["Python", "R", "Java", "MySQL", "CSS", "Bootstrap", "HTML", "C#", "GitHub"];
Console.WriteLine(languages[2]);

Java

How does this simple C# array work? Here’s what you should keep in mind:

  • C# arrays are created like Java arrays; both types of arrays are created using this structure-value type[] arrayname = [elements];.
  • If there’s anything C#, Java, and Python arrays have in common, it’s the 0-indexing system (as in, the first element of an array is always index-0).

In this example, I created an array that stores all the programming languages I’ve covered on this blog and accessed the third element of that array by using languages[2]. In this example, the third element of my languages array is Java.

[“C#”, “arrays”, “sorted”]

Now that we’ve explored basic C# arrays, how can we sort them alphabetically? Take a look at this example:

Array.Sort(languages);
Console.WriteLine();

foreach (string l in languages)
    {
    Console.WriteLine(l);
}

Bootstrap
C#
CSS
GitHub
HTML
Java
MySQL
Python
R

Now, how did I manage to sort the languages array? C# has a method for array sorting aptly called Array.Sort() which allows me to sort the languages array alphabetically.

As for the foreach loop, consider this a preview for my eventual C# loops lesson (and don’t worry-I’ll explain the idea of loops in greater detail). If you understand the idea of loops from other programming tools, you could probably guess that the foreach loop here is basically iterating though the sorted languages array and printing out each element one by one in alphabetical order.

Now, what if we wanted to sort this array the other way around-i.e. in reverse alphabetical order? Here’s how we’d do so:

Console.WriteLine();
Array.Reverse(languages);

foreach (string l in languages)
{
    Console.WriteLine(l);
}

R
Python
MySQL
Java
HTML
GitHub
CSS
C#
Bootstrap

It’s literally like what we did before-the only difference is that you’d use the Array.Reverse() method to sort the array in reverse order. And just like that, we see all the elements in the languages array printed out one by one in reverse alphabetical order.

  • Also, no need to worry about the Console.WriteLine() lines in this code, as I just used them to space out the output between examples since I’m using one script for each example.

[“C#”, “modifying”, “arrays”]

Last but not least, let’s see how we can modify arrays. Let’s change an element inside the array:

Console.WriteLine();
languages[2] = "JavaScript";
Console.WriteLine(languages[2]);

JavaScript

In this simple example, I changed the third element (index-2) of the array from Java to JavaScript. Pretty simple right?

Now, you may be wondering if we can add or remove elements inside a C# array. The simple answer to that question is sadly, no, because unlike arrays in Python (and perhaps other languages too), C# arrays have a fixed size, which means you cannot add or remove elements from an array once it’s been declared in C#.

[“C#”, “array”].length()

Last but not least, let’s see how long our C# array is:

Console.WriteLine();
Console.WriteLine(languages.Length);

9

By simply using the .Length method, you can see how long your C# array is. In this case, the languages array is of length 9.

As always, here’s the script that I used for this lesson-https://github.com/mfletcher2021/blogcode/blob/main/1darray.cs.

Thanks for reading,

Michael

Leave a ReplyCancel reply