C# Tutorial 2 Looping Arrays StringBuilder

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello internet and welcome to part 2 of my C sharp tutorial in this part of the tutorial we're going to talk about a raise for for each string builder casting and a whole bunch more I'm going to be covering all of these topics with a lot of detail and like always all the code and a transcript of the tutorials available in the description and so I have a lot to do so let's get into it alright so in the last part of the tutorial I showed you how to use marin - if you're on a Macintosh but right now we're just going to focus hundred percent on Visual Studio like I said before everything is exactly the same now I just started a brand new project and everything here is exactly what you should see you do however want to make sure that you have this guy right here in your code because we're going to be working with string builders and that is needed also you're going to need to import another module and this one is going to be system globalization and this is going to help us with culture specific formatting which you're going to see later on in the tutorial and that's all you're going to need to import for now now something I forgot to talk about in the last part of my tutorial is something that is called implicit typing now you don't always have to but you normally should go and define what type of data type you want to use for your variables but you can also come in and do something like this var and then let's say whatever this you don't have to use this name obviously and you go and you create a number now C sharp is going to be smart enough to realize this is an integer and it's going to automatically make it an integer type and just to prove that that is so I'm going to come in here now and I'm gonna go write line in Val type and I'm not sure if I told you last time or not how to get the data type for a value or for a variable but this is how you do it you go get type like that and it's going to output whatever the data type is for that guy and if we run this you're going to see that indeed whoops I was silly I need to come in here and make sure that that guy is going to stay open and of course I'm going to do that exactly as I always do by just going in here and going read line like that now if we run it you're going to be able to open it and see that it knows that it's an integer based off of the data that was assigned to the variable so that is implicit typing and I just want to make sure that I covered that so that you did know how to use it but like I said it's better to actually define the data type yourself and now we're going to talk about arrays so arrays are just boxes inside of a bigger box and they're going to contain many values of the same data type and each item is going to be assigned a key starting at zero and then incrementing up from there so the very first item you put in your array is going to be 0 with it's going to have the key of 0 and then one and two and three and so forth and so on now arrays are going to be all of a fixed size and to define one let's say that we have an integer array and let's say favorite norms is what we're going to call it we're then going to say a new there's a couple ways to define this and you're going to find the size for your array and that's the biggest your array is going to ever be if you want to use an array that has a dynamic size you use lists but this is how arrays work now what you're going to be able to do now is go favorite nomes and if you want to get the very first item inside of there or assign a value to the very first one of these guys let's say 23 that is how you do it and now we can come in here and we can go and retrieve that same value out of there let's say let's go favorite numb and we'll say 0 and if we want to get the value that is stored inside of there we're going to basically do exactly the same thing will go fav nomes and 0 once again and if we run that you'll see the 23 does indeed show up all right so that's how we put values inside of there as well as get values out of there we're also going to be able to come in here and assign values whenever the array is created so let's say we want to create a string array and it is going to contain customer information so we'll say customers between curly brackets we would then define all the different values that are going to be assigned in side of this guy and there we go and that is automatically going to make it the size of three to accommodate those values that you assign to it you're also going to be able to use var once again to define an array without an explicit datatype just as long as everything has exactly the same data type so it's going to have to all be integers all be strings all be whatever so then you could also come in here and do this and that would also be a valid way for us to create a string array one thing we can do however is we could actually create an array and put numerous different different types of data inside of it by saying it's an object array and the reason why is the object type is the base type for all other types inside of c-sharp so we would be able to say object and we'll say random array is equal to and then inside of here we could say Paul and we could say 45 and we could say one point two three four so there you have a string and an INT and a double or a dozen or whatever we're then going to be able to come in here and it's automatically going to figure out what type of data we have inside of it so we'll say random array and 0 in this situation and then come down inside here and go random array and let's go and get the very first one and then let's go and get whatever the data type is and don't try to memorize all this stuff just use the code I have in the description as a cheat sheet to write your own code and if you do that long enough you're just going to learn all this stuff and if you run it you're going to see that comes back as a string and if we would go and change this into two and run it again you're going to see that it comes back as a double alright so pretty cool stuff you're going to also be able to get the number of items inside of your array let's come into another one of these and we'll say array size and then we can come in and go random array whoops at the wrong button random array and length let's go do a couple other different things here so that we can see them all at one time you're also going to be able to cycle through an array and there's a couple different ways I'm going to show you how to use for as well as how to use for each and we're going to start off with zero because remember the very first item is has the key of zero and you can see here how the length is going to be useful because we're going to be able to automatically no matter what array is passed to this we'll be able to cycle through all of the items in any array by just referring to the length of that array and then we can come in here and do some different stuff let's say that we want to say array and we want to put whatever the index for it is inside of here and then afterwards we want to put the value that is assigned to that index then come down here and just go I like that and then random array like that and I like that and if we run that indeed you're going to see not only does it give us array size but it's also going to cycle through the array and go to get all that array information so another useful thing to be able to do we're also going to have things that are called multi-dimensional arrays now whenever you define an array like for example if you go our names and five this means that we are going to have five boxes and they are going to be stacked on top of each other vertically up and down so that's what that is going to be now however if you would instead come in here and do something like two two what that's going to do is with this first one just going to stack vertically up and down and then this is going to say that we want to total values from left to right or horizontally and I'm going to show you an example of how that's going to look so what we're going to do now is we're going to define array and it is going to be two rows high and two across so we will say string and then throw a comma inside of there and I'm going to say customer names is equal to new and then this is going to be a string and then I'm going to say that I want it to be two by two like that then inside of here if I wanted to find a multi-dimensional array directly inside of there I can say Bob and Smith and then after that curly bracket put another curly bracket and say something like Sally and Marx whatever okay so there we go now we have a multi-dimensional array now if we want to get a value outside of that multi-dimensional array what we can do is come in here like this and we can say something like MD value and then get whatever value we want out of there and then come down here and go customer names and get value and we want to get whatever is in the 1:1 position once again run that and you're going to see that Marx comes back why is that well we have Bob's in the zero zero position Smith is going to be in the zero one position because it moves over one then we're going to have Sally's going to be in the one zero position and then Marx is going to be in the one one position and that's the reason why Marx came back as that we're also going to be able to cycle through multi-dimensional arrays using for loops and much the same way so we're going to say something like for int I is equal to zero and we're going to continue cycling through as long as I is less than customer names and then we'll go get length which is a way to get the length of a multi-dimensional array item and then we'll increment that by I then inside of here we're going to create another for loop and I'm just going to copy this guy right here because it's going to do exactly the same thing so paste that there call this J call this J if we want to get the second part of our multi-dimensional array we're going to put a 1 inside of there we're going to increment this guy right there - J and then to output this information I'm going to go console I'm actually going to change this to write instead of write line and I'll get rid of this all together come down here tab that in and then to get this item out of here I'm going to go customer names like that and I'm going to go eye J inside of there inside of these square brackets and then after that let's just go copy that paste this inside of here and I'm going to do a right line with nothing inside of it which is just going to give us a new line save that run it and now you can see that it prints out Bob Smith and Sally marks right like that I could have put a space inside of there but I didn't so no big deal just so you know and this is something you can play with on your own let's say that you would go and go array names like this and two and two and three what this is going to do this first part remember this is vertically up and down the second one's going to say how many items horizontally and then you can think of that as just a matrix or a spreadsheet or whatever and then this guy is going to tell you how many of these are going to be created so this is pretty much like saying how many sheets of 2 by 2 matrices are we going to create and that's something you can play along with on your own and that will give you a better understanding of how arrays work now what I'm going to do is come in here and also show you for each so let's go and create another integer array and I'm going to call this random number and 9 and 2 I also want to show you that we would actually be able to come in here and pass an array to a function without any problems so let's say that we want to do something like print array and random number it and then we want to have like a descriptive phrase it's going to show up in the output ok so that's going to give us an error because it says what's print or I don't know so let's go and create that so outside of main we're going to come in here and I'm going to say static and void and print alright and it is going to be receiving an integer array and I'm going to just call this int array and then it's going to be able to it's also going to get sent some type of message so I'm just going to go like that and then what it's going to do is cycle through using for each like I talked about before so we can say with for each for each and then int K in whatever the array was passed in here so that's into right and what it's going to do is temporarily store each value from the array in K and then we'll be able to manipulate it down inside of here and how we're going to manipulate it and how we're going to be able to manipulate it is consul and what I want to do probably right line and then we're going to put the information inside of here so that's going to be whatever the array value is and then afterwards I am going to throw a message inside of there so well let's put the message first and then let's put whatever the value for the array is alright so there's that guy whoops change this into and put the K up there must accidentally Lily did that all right so that's saved and if we run it you're going to see one four nine two prints out okay so good stuff now we're also going to be able to come in here and sort array information so let's go and do that very easy to do we're just going to go array and sort and then pass inside of it whatever our array is going to be so random number on that and you're going to say that it now puts them in numerical order so that's useful to be able to do we're also going to be able to come in and go reverse like that run it again now you can see they're in reverse order from how I put them inside of there we can also get the index of whatever we are searching for or a negative one if negative one comes back in that means that nothing was found and for this guy let's say we want to search for one so we'll say one is that what index and this is going to return some type of a value for us and to search through it we're going to go index of and then we'll say what specifically are we going to be searching inside of what's the array name and what are we specifically searching for which is going to be the number one run that you can see it comes back as zero index and you can also see it's in the zero index right there we're going to also be able to change the value at a specific index so let's say that we want to change it we can go random nomes and go set value and let's say we want to change whatever is in index one to the value of zero we can go zero comma one and we're also going to be able to come in here and copy part of an array to another one so let's go and create an integer array and let's call this the source array just so it'll be easy to see what we're doing here and we'll go and put some values inside error so one two and three we're then going to create a destination array which is where we're going to be copying information into and here I'm just going to create an empty array so I'll go and say it's going to have a total capacity of or size of two I'm going to put starting index is going to be equal to zero once again just so you can see what's going on and the length is going to be equal to two now what we're going to be able to do is come down inside of here and say array copy and source all right where we're going to be getting the different pieces of data from starting at a specific index then we're going to define the destination array and the starting index and the length we can then come in here and call our print array function that we created a second ago pass inside of it destination array and then just to see exactly what we are working at or what we're looking for put copy inside of there and you can see that it copied one and two over into the destination array you're going to be able to create arrays in other ways as well another way is you can use the create instance function here we can just say array and let's call this another array because I'm running out of names that I can use for my array so we're going to be able to say create instance and then we could say type of and let's create an integer array and then after that say that it want want ten spaces total inside of our Iraq we're then going to be able to copy values in our source array to our destination array and how we can do that is go source array copy too this time we're going to be copying into another array this new guy that we just created and what we're going to do is copy the entire array over and we're going to start putting values in at the 5th index inside of another array and then we will use for each to come in here and cycle through all of those different items in another array and here we're going to go and output that information so this is to demonstrate copy 2 and we want to get each item out of that Rea and run it and here you can see that it started putting in at the index number 5 so 0 1 2 3 4 5 and it put in all three values from our source array into that new another array that we just created another thing that's really cool is we are going to be able to search for an element that matches certain conditions that we are going to define something called a predicate so I'm going to go and create another integer array and I'm going to call this number right and inside of here I'm going to go 1 and 11 and 22 so there is an array and then what I'm going to do is create the predicate or what it is is it's going to be a function that's going to return true or false depending upon certain conditions let's go down here and create a new function and I'll make this private static it's going to return true or false like I said before I'm going to say greater than 10 is what this is going to stand for and it's going to receive a value and then it is going to return true or false whether this value that was sent in or not is greater than 10 ok so pretty simple function so let's come up here and use it ok so right after this guy I am going to come in and go console.writeline and we'll say greater than 10 and it's going to come back true or false and I can go array find numb or is what I'm going to search for and it's going to come back with the very first match so greater than ten is the name of the function or the predicate that I defined and if we run it you're going to see that 11 comes back doesn't get the 22 because it only returns the very first value and of course it would return nothing if none of the matches and it's important to know on your own go and you can do pretty much exactly the same thing with a function called find all so instead of find use find all and the only difference is find all it's going to return an array of all the matches and another function called find index is going to return in index of the match okay so that is a run-through of a whole bunch of different things we can do with race now what I want to do is take a look at string builder okay so we covered strings in the last part of the tutorial and something that you probably are not aware of because I didn't talk about it is anytime you go and make any changes at all to a string you are actually creating a brand new string and this can can become very inefficient if you are working with large blocks of text so what we have available to us inside of Steve sharp is something called a string builder and what it allows you to do is to actually go in and change a block of text without creating a brand new copy which is what you do whenever you're working with strings so how you create a string builder is you just go string builder and I'm just going to call this s B and you go equals to new string builder and then you define whatever text you want to put inside of it and it's automatically going to give you 16 characters of space if you do not define how much space you want if you however would like to define how much space you get you would instead go and do pretty much exactly the same thing in a string builder like that and here we'll say something like more stuff that is important and then after that say that we want to set aside 256 characters worth of space and make sure you always put the new inside of there because it is an object and we'll get more into objects here very very soon we're then going to be able to come in here and get to the maximum size so let's go it's called as capacity and then let's go and get the capacity so we can get the capacity for well let's just do both of them so capacity like that and then let's get the capacity for the second ones so that you can see indeed that I was right so we'll go string builder two and we can run it and you can see 16 is the default no matter how many you put in there and 256 comes back because I forced it to be on 256 likewise you're also going to be able to get the length or the number of characters that are inside of there so you can just go lengths like this and then change this to length and run it and you're going to see it comes back as 11 because that's how many characters you have assigned inside of this string builder up here you're going to be able to add text to a string builder and let's just go SB to just by going append line and you can throw new lines or whatever you want to put inside of there so we'll say more important text and close that off you're going to be able to define culture specific formatting and this is what I was talking about way up here this guy right here globalization that's the guy that we are going to use for this so we're going to go culture info and I'm going to say that this is English specific to the United States and then culture info create specific culture and remember I said English - us and now what we'll be able to do is use formatting so we can do something like string best customer is equal to Bob Smith and then go sb2 and appends format and English us and then we can say best customer curly brackets and then follow that up with best customer and that's going to append that formatted piece of information and make sure you put your quotes in the right place otherwise that's going to cause an error all right you're going to be able to output your string boat builder in much the same way so we can just go SB to let's just get rid of this all together and go SB to two-string and run it just you can see what it is now you see more important texts all that stuff was added in and best customer Bob Smith also shows up will also be able to come in and replace a specific string inside of there by going SB to and replace and what we're going to replace specifically is text and change it to characters I mean here and we can print that again just to make sure that works going to be able to come in and clear a stringbuilder just by calling the clear function on it will then also be able to append some text to it so we'll go random text we could also compare over to see if equality exists between two of these objects by going SB and equals and SB - guess what that's going to come back as false we're also going to be able to come in and in insert a piece of text in a specific index so let's say that we want to start at index 11 and put a string in that says that's great like that and it's throw space inside of there so that shows up inside of the output and we can run this just to see how that works you can see random text that's great shows up there and also false for whenever we check to see if they were equal or not we're also going to be able to remove a number of characters starting at a specific index so we could go SB to and remove and say we want to start at index 11 and remove the next seven characters after that and sure enough if we then come in and paste that in there and run it you're going to see that that's was removed out of the string builder alright so that is a rundown of what you do with arrays and string builders and loops and a whole bunch of other different things and I have a couple more minutes so let's go and cover one more topic and that topic is casting very often it is very useful to be able to cast from one type to another so I'm going to do is I'm going to create and that just means castings it's a fancy word for converting so let's go and let's create a long here and it's going to have the value of one two three four and let's go and let's create an integer and let's assign it well let's let's first off let's go and try to assign num1 to that to see what happens you can already see there's an error and it says right here cannot implicitly convert type long to int so how do you solve that very easy just come in here like this and go int and your error goes away and we can come in here and verify that everything is peachy keen and indeed this is an integer by just going in and it'll say something like original and cast and then for this one we'll go num1 get type c and get type already before and then we will say num2 and get type again because that's awesome and we will run it and we'll see that the original was a 64-bit integer which is a long and here it is a 32-bit which is an integer all right so there you go guys a whole bunch of random topics I covered today and I'm pushing forward to cover the entire language as quickly as humanly possible and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 115,401
Rating: undefined out of 5
Keywords: C# Tutorial, C#, Learn C#, C# StringBuilder, C# Arrays
Id: bBG2o905sRQ
Channel Id: undefined
Length: 27min 51sec (1671 seconds)
Published: Wed Jan 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.