Locked lesson.
About this lesson
Appending Arrays in C# is a bit different than in other programming languages.
Exercise files
Download this lesson’s related exercise files.
25 - Appending Arrays.docx61.1 KB 25 - Appending Arrays SOLUTION.docx
58.2 KB
Quick reference
Appending Arrays
Arrays may be appended when you want to add something to the end.
When to use
Use this method to append an array.
Instructions
using System.Linq;
string[] toppings = new string[3] {"Pepperoni", "Mushroom", "Onion"};
toppings = toppings.Append("Green Peppers").ToArray();
Console.WriteLine(toppings[3]);
Hints & tips
- Don't forget to import: using System.Linq;
- toppings = toppings.Append("Green Peppers").ToArray();
- 00:04 Okay, in this video I want to talk about appending arrays.
- 00:06 And this is a touchy subject because I told you a couple of videos ago,
- 00:10 we have to be very specific when we create our arrays.
- 00:13 You can see right here, we're saying, hey,
- 00:15 we're going to put three things in this array.
- 00:17 And I come from a Python background and this just rubs me the wrong way because in
- 00:21 Python you don't have to tell it how many things you're going to put into array.
- 00:24 You can slap something on the end, you can remove things, you can change all around
- 00:29 whenever you want, and it's very easy, not so much with C sharp.
- 00:32 So like I said, in this video we're going to talk about appending array, so
- 00:36 here we've got 0, 1, 2 items, so three items in total.
- 00:40 Let's try and add something to the end of this.
- 00:42 So let's go toppings and we know 0, 1, 2.
- 00:47 So let's try to add something at position three.
- 00:50 Now we've said there's only three items and
- 00:53 here we're trying to add a fourth item.
- 00:55 And now this is a little weird because three represents the fourth item,
- 00:59 because again, arrays start at zero.
- 01:01 So to add a fourth item it would be toppings three and
- 01:05 here it let's just call this what, peppers like that, I don't know.
- 01:09 So let's come down here and actually we could just copy this guy,
- 01:13 put them in there, save this, run it, what do you think is going to happen?
- 01:17 Angry, everything is orange and there are errors everywhere and
- 01:22 the whole thing is just come crashing down, not good.
- 01:25 So let's go ahead and close this out and what's going on here.
- 01:28 Well, like I said,
- 01:29 you've to explicitly say how many items are going to be in your array.
- 01:33 You try to add one more, or 5 more, or 100 more, or a million more, you got problems,
- 01:37 it just doesn't work.
- 01:38 So C# expects there to be 3 things in this array and that's all there is to it.
- 01:43 Now there's several different ways you can get around this.
- 01:46 There are things called lists that we're not really going to talk about right now.
- 01:49 You can create new arrays.
- 01:51 You can take your stuff from your old array, add it to a new array where
- 01:55 you've defined it to have more items in it, all kinds of things.
- 02:00 I'm going to do something that's much easier.
- 02:01 We're going to come up here to the top here, and
- 02:05 we're going to call using a System.Linq, and this is L-I-N-Q.
- 02:10 And this will allow us to sort of pseudo append something to our array.
- 02:16 So here we can come down here and let me just comment this out.
- 02:20 So what we can do is, we can take our toppings and
- 02:25 we can set that equal to toppings.Append.
- 02:29 And let's say we want to add green peppers, right?
- 02:33 And then we want to go .ToArray and that's a function, and that should work.
- 02:39 So, technically we're kind of creating a new array and adding this to it sort of.
- 02:45 It's an empty sort of thing.
- 02:46 Now here we can see, we're going to keep the third item here.
- 02:50 So 0, 1, 2, 3 and this should work.
- 02:53 Let's go ahead and save this and run it.
- 02:57 And we see sure enough green peppers.
- 02:59 Now we can also come down here, and let's console logout.Length.
- 03:05 So up here before we append this, I'm going to console logout the length, and
- 03:10 we're going to add something and look at the length again.
- 03:14 So if we save this and run it.
- 03:17 We see 3 green peppers and then 4.
- 03:21 Now originally of course, we had 3 and then we added green peppers.
- 03:25 So now the length of the array is 4, it has been changed.
- 03:28 It has been appended and that's how we do that.
- 03:30 So a little bit sloppy,
- 03:31 at least a little sloppier than some of the other programming languages out there.
- 03:35 But this will get the job done.
- 03:36 Just remember to use System.Linq,
- 03:39 that allows us to use this data pin thing to this array.
- 03:44 And then we can add anything we want just like this.
- 03:46 So that's all for this video.
- 03:47 In the next video, we're going to look at multidimensional arrays.
Lesson notes are only available for subscribers.