Join Bruce Van Horn for an in-depth discussion in this video Managing ordered data with lists, part of C# Essential Training: 2 Flow Control, Arrays, and Exception Handling.
- [Instructor] Arrays are great for a lot of applications. They are fairly fast, they're light on resources, but they don't have a lot of functionality built into them. So generally speaking, if you want to work with arrays, it's because you're doing something specific and you've got a lot of array logic at your disposal that you've written yourself. What I want to show you next is a much more capable class that works in a way that's very similar to arrays but gives you a lot more facility.
So let's go back to our C# interactive prompt. I'm going to go ahead and close my test window here to get it out of the way, and I'll select View, Other Windows, C# interactive. And now I've got my interactive window. It was actually already down here in a tab, but it's good to remember where it is. Just to make sure there's nothing in it from an earlier exercise, let's go ahead and reset it. So I'll click the Reset button at the top, and I'll go ahead and click Clear Screen so that we have a fresh window to work with.
So the lists in C# are actually part of a class called generic collections. And I'll tell you what that means in just a moment. For now, let's take a look at creating one. I'll create a variable, and I'll call my variable awesomeSauces, and I'll set that equal to new list of strings. Now this looks a little funny, right? It's go this little diamondy looking thing down there. You can see it and you've probably noticed these in some of the property shopping that we've done. These angle brackets are telling us that this is a generic which means that this is a class inside of C# that can accept a type as part of its declaration.
So again, I'm going to make this a list of strings, and this means that I can only put strings into it. Just like earlier with arrays, we declared an array of strings. Well, here we're declaring a list, and we're passing in a type. So any time you see those angle brackets, you're dealing with a generic and it means that you can pass in any kind of class that you want to there including ones that you make yourself. All right, so we've instantiated our list. And before we had sort of this funky syntax to add things in or we could do it by position.
This is a little easier. I can actually add some awesomeSauce right in here with .add. Now before I jump right to .add, let me back up a little bit hit the dot and let's property shop a little bit. Now that you know what that diamond means, you can see that there are some diamond functions in here. These are actually part of something called link which we'll cover later. The more interesting ones for now are going to be Add, AddRange. If there's an Add, then there must be a Remove. So if we scroll down far enough, we'll find Remove, RemoveAt, RemoveAll.
There's a Reverse which will flip the order of the items in your list, and there's also a Sort, very handy. For now, let's add some awesomeSauce to our awesomeSauces list. And let's add in some awesomeSauces. How about some Tobasco, maybe some Cholula. And you'll note that I never had to tell it how many things I was going to put in here.
What's happening here is each time I add, I'm actually just resizing that list and it dynamically expands as I add things. You can actually store quite a lot in here. I've written a number of programs that required me to store tens of thousands of elements and then process them and it's fully capable of handling that kind of load. To get elements back out again, you can use the same technique that you use for arrays. So if I wanted to see what was the first item in the list, in my awesomeSauces list, I could do awesomeSauces zero in square braces and it will give me back Tobasco.
And again I mentioned Sort, so I can do awesomeSauces.Sort. And this is going to act on awesomeSauces. It's not going to return something. A lot like that reference thing we saw earlier, it's going to affect awesomeSauces directly. So be careful when you use this because it's actually going to change the value of awesomeSauces. Let's try our awesomeSauce, the reference again at position zero, and it should now be in alphabetical order. So this time we get back Cholula because it changed the order.
Cholula is alphabetically before Tobasco. So lists are highly amazing, highly useful classes that you can put into your programs. Don't feel constrained to using just primitives here. You can use any class that you like.
- 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