From the course: C#: Applied Data Structures

Basic C# data structures

- [Instructor] Let's start by taking a look at some of the basic built-in data structures in C-SHARP before we examine some of the more complex data structure classes. For some of you, this might be a bit of a simple review. So if you're already familiar with strings, arrays, and tuples, you can skip this video. First, let's consider strings, and I'm going to open up my program .cs file which is in my basic folder in chapter one, so you can follow along. A string is a collection of characters and strings are immutable objects. In other words, they can't be directly modified once they are created. So strings are declared using the string keyword. So I'll create a variable named s1, and I'll give it the value of Hello World with a space. And you don't need to use the new operator, unless you're creating a string from an array of characters. So I'm going to uncomment this array of characters declaration right here, and then I'll make another string called s2. And in this case, I'll type new string and then pass in the character array to create my string. Now, it's important to understand how immutability affects strings. When you make a change to a string in dotnet, so for example, I'm going to add s1 and s2 together. So I'll just write s1 plus equals s2 and then we'll go ahead and print that out. And then let's also convert s2 to uppercase. So I'll call s2.ToUpper. All right, and we'll print that out as well. Okay, let's save and then let's run this. So here in Visual Studio Code, I can just right click on the folder that my code is in and choose open in integrated terminal, so I'm going to do that. So that opens my terminal right in that folder, and I'll just write dotnet run. Okay. And you can see the results here of the plus equals operator and the ToUpper function. So each one of those results, so when I do this plus equals operator, and I call ToUpper, what happens is each of those operations returns a copy. They return a new string. The original string is not directly modified. And what that means is you have to be careful with string references. So for example, if I create another string, I'll write string three string, yeah, s3, and I'll give that a value of Hello with a space. And then let's imagine I assign another string reference to this one, so I'll have string, s4 is equal to s3. And now what I'm going to do is I'm going to modify s3. So I'll write s3 plus equals World. And then I'm going to write out s4, the one that I made to point to s3 right here on line 16. So when I run the result, so I'll run again down here in the terminal, notice how s4 still points to the original string reference which just has Hello in it. So even though I added the World to s3, s4 was not updated, even though it's pointing to s3 because this operation right here creates a new string. It does not use the old string reference. Okay, let's look at arrays next. Arrays are contiguous storage in memory of the same variable type. So to declare an array of integers, I use the bracket syntax. So I'll write int and then two brackets, and I'll call my variable nums, and then I'll write new, and then again the data type. And then inside the brackets, how many of those that I want. I can also initialize the array with a set of initial values if I want to. So I can write int, and then I'll make another variable called nums2. And in this case, I'll use the initializer syntax in between the two curly braces to just put some numbers in the initial array. Arrays can also have their values implicitly typed using the Var keyword, and the compiler will infer the type from the data. So for example, if I wrote var a equals, and then I'll use new with two brackets, and then I'll create some initial data. So A, B, C, right? D, E, F, and then, G, H, I, okay? That's going to create an array of strings because even though I'm not supplying the data type, I'm just using Var, the compiler will look at the data and say, oh, those are strings. And this must be a string array. Now, if you want to have multiple data types within the same array, what you can do is create an array of objects because object is the most base class in dotnet. Everything in dotnet basically descends from the object base class. So this would let me create an object array, and I'll name it objs. And inside the curly braces, I'll put some initial data. So I'll put a string, and I'll put a couple of numbers, and then maybe a Boolean or two. All right, and so, when I run this, let's go back up here and run this again. And you can see, now, I'm not printing anything out, but you can see the string results and there's no compile error, so everything must be working. Okay. Finally, let's look at tuples. So tuples are lightweight data structures, and they're designed to group together multiple data elements without having to define a class. There are several ways that you can create and use tuples. One way is to define the data types that they will hold. So let me scroll up here a little bit. What I'm going to do is create inside two parentheses, I'm going to write some data types, a string, an int, and a bool, and then I'm going to name my tuple variable t1. And then inside another set of parentheses, I'll actually provide the data. So the first has to be a string, the second one has to be an int, and then the last one has to be a bool. I'll make that false. And then I will print out my tuple one variable. You can also name the individual fields in the tuple. Oh, whoops, actually, first, before I'm going to print this out, I'm going to print out a string, and I'm going to use a format string for that. Now, when you're working with tuples, if you don't name the items inside the tuple, the way that they're accessed is using this item syntax. You can see that when I type the dot on my t1 variable, I'm getting statement completion for Item 1, Item 2, and Item 3. So I'm just going to print out Item 1, and then I'll print out t1.Item3. But you can name the individual fields inside the declaration if you want to. So I could have written string s, int i, right? And bool b, and done pretty much the same thing. And I'll just copy the same data and put it down here, and then I'll just print this out. So what I'll do now is inside my print statement. Now, when I write t1, when I hit the period, notice that, oh, whoops, I'm using t1 there. There we go, t2. When I type t2 and then period, notice how I'm getting the variable names now. So now I'm getting B, I, and S, rather than Item 1, Item 2, and Item 3. So I'll just do s and then I'll do t2.b, all right? And of course, you can use the Var keyword, and if you want, you could put the names inside the initialization code. So I can just write var t3. And then when I type the data inside these parentheses, the compiler will infer that this is a tuple. So I can write s: for example, and then the string, and then i:25, and then b:true. And when I do that, the compiler will say, okay, this is property named S, that's a string. I is an integer, and B is a boolean. And then let's copy and paste this right line. And once again, let's write out t3 and t3. All right, so let's go back up. Let's comment out the previous example. So I'm going to just select these console right lines and comment them. And let's go ahead and clear this and then run. And there you can see the results of those three tuple statements being printed out in the terminal. So tuples are really useful for a variety of scenarios, and actually one of the most common is to return multiple values from a function. All right, so now that we've seen some of the basic data types, we can move on to some of the more complex data structures.

Contents