Join Bruce Van Horn for an in-depth discussion in this video Simple arrays, part of C# Essential Training: 2 Flow Control, Arrays, and Exception Handling.
- [Instructor] Arrays and collections are one of the most useful features in C#, and really in any programming language. They are one of the fundamental building blocks that every programmer needs to master. So what exactly is an array? The easiest way to figure this out is to get up, go into your boss's office and say, boss, I want to talk about arrays. Pause for effect. And now that I've told my cheesy joke, I can go back to talking about what an array really is. An array is essentially a way of storing more than one value in a variable at one time.
Why don't we play around with this concept a little bit. Let's go into the C# interactive window. For this you want to do view, other windows, and towards the bottom you should see C# interactive. I'm going to make my window quite a bit larger so that we can see it. And just in case you've never used the interactive window before, effectively what you're looking at here is what we call a REPL. REPL is R, E, P, L. It stands for read, evaluate, print, loop.
Many languages have some sort of REPL in them. Two of those most popular right now being Python, and NodeJS. Essentially what you can do here is you can type expressions. So I can type in five plus five, press enter, and I'll get a result. So what's happening here is, the environment is reading, that's the R, it's evaluating, then it's printing, and then it goes back to the command prompt, and that's the loop part. So why don't we create ourselves an array.
To do this, I'm going to type new, string, and here's where the magic part comes in. I'm going to use an open and close square brace. That's how we denote that this is in fact an array, as opposed to just a string, which would look like this. Open and close brace means you're working with an array. Arrays in C# have a fixed size. So generally you're going to be setting a size here when you instantiate the array. This means we have four places in our grocery list. I'll go ahead and press enter.
And now I'll go ahead and add some things to our grocery list. You do this based on an index. An index is just the position in the list. So the first thing in our list is going to be milk, so I'll say grocery list position zero, which is the beginning of the list, and I'll set that equal to milk. Next I'll add a few more things. And each time I'm going to move the index up by one. So, the second position, which is actually number one because we start counting at zero, is going to be cheese.
Let's add some eggs. And finally, some apples. So there's my grocery list. If I try to add one more to it, what do you think is going to happen? Well, we declared it as having four positions, so something bad is probably going to happen. If that was your guess, you would be correct. If I try to add a fourth element here, maybe some bread, I'm going to get a error.
It's going to say index was outside the bounds of the array. So what it's saying is, you said that you're going to have four things in your array, but then you tried to put something at position four, which is actually the fifth position, and that's no good. That's not going to work. So the things to remember about arrays in C# are that they have a fixed size, which you set when you actually create the array, and the arrays are strongly typed. They're type specific. There's no way also for me to add something in here that is not a string.
Your visual clue for arrays is the same as it is in many other languages, square brackets. And the square brackets will contain an index, which is the position of the element on the list.
- Writing unit tests in C#
- Working with simple and multidimensional arrays
- Managing ordered and unordered data with lists
- Evaluating conditions with if-else statements
- Using OR, AND, and NOT operators
- Building loops
- Debugging and handling exceptions
- Creating the final build of your C# project