C# Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part one of my co-op video tutorial you guys told me you wanted to see a c-sharp Samarin and game tutorial series and this is where it's going to begin in this part of the tutorial will provide an introduction so that you will know how to install c-sharp on windows of course as well as on Mac OS and I'll also cover a lot of basics like input/output data type strings functions looping and a whole bunch more like always all the code is available in a description underneath of the video and I have a lot to do so let's get into it alright so I'm mainly going to be developing on Windows but I didn't want to leave the Macintosh people behind so if you want to do absolutely everything I'm doing here instead of the downloading Visual Studio which is what we're going to do on windows instead you're going to download as a Maron at Samarin comm download and all you do is type in your name and your email address and a company agree to the terms click on download it installs and everything's great once you install it this is what you're going to see and if you want to create a new project just go up here to file click on new solution this is on Mac OS but like I said I'm going to be developing almost everything in these tutorials using Windows doesn't matter everything's exactly the same then you're going to click on dotnet and for now console project and then click on next I'm going to call this sample app and I'm going to click on create and just to show you that everything is going to work here is everything right here and if you want to run this guy just come up here click on that guy right there for the play and you can see that the console opens and runs alright so there you go Mac users that's what you're going to do now onto the windows users now on windows I am going to be using visual studio comm and I'm specifically going to use Visual Studio 2017 which is the release candidate and I trust you'll be able to download this just click on free download install it and it will be ready to go and now I'm going to jump over and start writing a whole bunch of code alright so here we are in Windows specifically Windows 10 and if you want to create a new project just so you know you're going to come over here click on file you're going to say new project like that and it's going to open up once again to start off we are going to have visual c-sharp right here like that and we are going to click on console application and you're going to give it a name and I'm going to call this guy c-sharp Tut like that that is going to be the name for it and I'm going to click on OK and create everything alright so after you do that this is what you're going to be presented with a whole bunch of different things these are going to be a whole bunch of different libraries that are going to provide objects and different functions with it we will be able to use they're very useful and this is what will open up by whenever you create a new project now up here where we see a name space you're going to use name spaces to define globally unique objects so that way if you combine your code with other people's code and the names are exactly the same you're going to be able to use this as a way of making your code unique class program is the default name that we have here for this guy and like always a class is just going to define the variables and methods used by objects and I'm going to get much more into classes and objects in a later part of the tutorial series right here you see the main function which is going to be where all the execution begins whatever code is inside of these two curly brackets is where everything is going to start off executing specifically static right here means that this function is going to belong to the class and can be executed without needing to create an object don't worry if you don't know anything about objects I'm going to get into them heavily void just means that this function doesn't return a value after it executes but it is very common to instead of having void here have int inside of there and in that situation it would return an integer whenever this function is done executing that integer value would normally be a zero whenever the function successfully execute or negative one whenever then error of some sort of curs but we're going to keep this as void and once again just means doesn't return anything and the main function here also as you're going to see is able to receive multiple values that will be stored in a string array and an array is just a whole bunch of boxes that contain values what we're going to do here first is just a standard whole hello world just to make sure that everything is working so I am going to go console.writeline and if we would use right it would not put a new line at the end if we put a right line it will put a new line at the end so let's just come in here and we'll demonstrate exactly how that works right like that and you save it and you click on starts and it executes and like I said Mac OS users are going to be able to automatically do all the same things but what did we see there it opened up but then it closed well a trick to keep the console open after whatever code we have inside here execute is to come in here and ask for input of some types and the input we're going to ask for is just whatever it's just going to be a return key and to do that we're going to put read line inside of there and save it and start it and now whenever the console opens it will not close until I hit return and you can see right there it says hello world all right so we can hit return it received input right here into this guy right here and it closed the application we're going to actually get real input here in a moment though so that you'll get to see how we can input information and then work with it now like I said right here the main function is going to be able to receive a certain number of arguments that are going to be passed to it and if you want to simulate passing command-line arguments into Visual Studio what you're going to want to do is over here in the solution Explorer that is what this guy is called what we're going to do is we're going to right click on this and then we're going to come down here and select properties this guy's going to open up right here and right here where we have debug we're going to put a couple values so I just put one with a space two with a space three with the space and four all right so that's all we're going to need to do and we can keep that open we can save it do whatever jump back over inside of here and if you're wondering how to pass those properties in twos Merricks also what you're going to do is go over here and right click on this come down here to options and then inside up here click on default and then type in these same arguments right there and click on OK and just a test that that works we can go in here create a for loop that's going to run through every single one of those passed in and print them out click on this guy and you can see it printed one two three all right back over into Windows demonstrate some more stuff alright so I'm going to go in here and do exactly the same thing and what we're going to do is we're going to loop through those arguments and I'm going to explain what's going on so I'm going to first off create a variable that is going to increase in value each time and incremented value and it's going to be an integer and I'm going to continue incrementing as long as the value of I is less than the total number of arguments that are in our array and that's how we get that we use the length field to get that information and then remember we have to increment now I plus plus is exactly the same as if I would go I is equal to I plus 1 all right it's just a shortcut way to do exactly the same thing once again we're going to come in here and we're going to go console.writeline and remember right would not have a new line and let's go and do something a little bit better here what we're going to do is we want to actually print out the specific what it's called index for the array item and we're going to go and print those out as we print out the values stored inside of them now arrays are going to assign values that are called indexes and that is how we're going to be able to get the value stored inside of each of the boxes inside of our array and the very first index or address for the value in our array is by default NC sharp is going to start at 0 so what I want to do is I want to come in here and not only get that value but I also want to go and print out the value associated with that index this is going to be the first value that's going to be transported into this final string and a string is just a series of characters so if I want to get the value of I I get I right there and if I want to get whatever is stored inside of the arguments array I just go like that then what I'm going to do is whenever this runs it's going to take I right here and put it right there inside the string and where it has one it's going to take this and put that right there so if we save this and run it you're going to see it does exactly that see argument one two and three and it prints out the values that are going to be stored inside of that guy but it's missing one of those values and that's because I had this sign 2 1 instead of 0 so let's run again and you're going to see how the indexes work out and how the values associated with those indexes in the array work out and print our information all right so there we are now we know how to use a for loop inside of c-sharp if you are wondering if there's other ways to get the array front in regards to the command-line arguments that are passed in we can actually create our own string array and we just do it like that and the little brackets that I mean it's an array instead of just a simple one string so I'm going to call this my args and you're probably never going to do this but I thought I'd cover it just to be covering everything and what you're going to do is go command line org and this is a capital G band line args and this is a method that will actually pass those values back to us now let's say I wanted to print out all of those arguments onto the screen as just one big giant string I could go right on and I'm going to use a function called joint string dot join and I'm going to join every single value inside of that array together and I want each of them to be separated with a comma and a space and then the array that I want to use here to output this information is my arguments which is this guy right here and we can run that to see if that worked out for us and you can see that yes indeed it did however what it also does is it prints out the location for your actual executable file and the benefit of that is now you know on your specific computer where the executable is for your application that you are making inside of c-sharp so kind of useful information nonetheless so now what I'd like to do is cover functions and functions just allow us to take our code and break it up into smaller and smaller pieces because you don't want a hundred lines of code that you can't read you want to try to keep it to about ten lines of code per function and what I'm going to do is I'm going to create a function that is going to be called say hello and it's going to receive input and then send a message to the user so I'm going to get outside of my main function and create a new function and I'm going to have this be private and I want this to be void it's not going to return anything static of course means that it belongs to our class it's not going to receive any arguments as you can see there's nothing inside of there and then inside of it what we're going to do is well it's good first off go and create a string and let's have this be name and let's give it a default value of nothing and then we're going to output some information on our screen so we'll go console and here I'm going to use write because I do not want any new line after this gets put on our screen I'm going to say what is your name put it like that and another space so that it looks nice and neat then I'm going to ask the user from information and I'm going to store that in the string that I created above so how we can do that is go console and read line and whatever they enter is going to be stored inside of this name string variable and then what I'll be able to do is go console and now I'm going to use write line and then we go create a little bit more of a fancy type of hello world thing and we'll go hello and then I want whatever the value of name is to be transposed directly inside of there and put it on the screen now however if I want to call this from my main function because remember all the execution starts in the main function I'm just going to go say hello and put that there and run it now as you can see prints out all that other information I can type in Derrick hit enter and it now says a little Derrick okay so that's how to get information as well as how to print it out we don't need all this right now so let's just delete that okay so now I'm going to talk about data types and data types are very important because you want to use as little space in memory as humanly possible so that you don't use up a ton of memory and all so that your application can run as fast as possible now to start off probably the most basic type of data type or what are called boolean 's and they only store values of either true or false so I'm going to have a boolean here and it's going to be can I vote and I'm going to mark that as true there you go you just created a boolean and of course you could have false in there instead but nothing else and then we have integers and they are probably going to be the variable data type that you use more than anything else and what they represent are 32-bit assigned integers and you may ask yourself well what exactly does that mean well let's go and I'm going to print out the largest type of integer that you can possibly have so let's go biggest integer these are just going to be values that don't have decimal places in them and to get the maximum value an integer can store you go int and Max value and why not go and also find out the smallest value you can have remember these are going to be signed so we will go smallest like that and to get the smallest we change this to min and if we save that and we execute it you are going to find out exactly the biggest integer you can store and the smallest integer of course you can't store anything smaller or anything bigger than those guys if however you would like to store something bigger there are also things called Long's and Long's are 64-bit signed integers and they are just going to have a long there instead of an int so let's say we wanted to find the biggest long like that and you define integers of course by typing in int okay so that's exactly like we did with the Boleyn's there we're going to do more with variables as we continue and let's go and get the largest long we can have and let's go and get the smallest long we can have and now with a click of a mouse you can see the biggest long and the smallest long alright so very very very big numbers that brings us to decimals now decimals are going to store one twenty-eight bit precise decimal values and they are going to be accurate to 28 digits and I'm going to show you an example here of how those guys are going to work for us so let's go and create a decimal and to do so you just go decimal and I'm going to go decimal pi value is equal to and let's throw pie inside of there and I want to make sure you see the M here you're going to have to put the M inside of there whenever you define decimals so there we have a decimal pi value then what I'm going to do to show you how accurate these are I'm going to go decimal and decimal big num is equal to and I'm going to throw in this number right here so let's put that at the end of it and then let's try to add those together and see what it looks like see how accurate they are so I'm going to go to console and write line and I'm going to say just make sure that you can see that this is a decimal then I'm going to go PI plus big num is going to be equal to and 0 inside of there and then you can also do addition inside here you don't just have values inside of there so we can go decimal pi val plus and then decimal big num and close that off and why don't we come in here and also print out the biggest decimal value we have so a decimal like that and to get the maximum decimal we're going to say decimal in uppercase letters and smallest decimal as well and decimal again got all those save we're going to learn all kinds of things here and here you can see now we added three points 0 0 0 0 0 and a whole bunch of ones you can see right here the value is 3/2 and we added one one at the end of it you're going to see that the one got on there but the next one is going to be the next one over and it's just completely disregarded if you count all these numbers up you're going to see that there are 28 digits so you will know that the maximum precision in regards to working with decimal values for a decimal datatype is going to be 28 digits and you can also see the biggest decimal and the smallest decimal now we're going to come in and work with another data type that is called doubles and to create a double just go double like that and this is a 64-bit floating type and that just means that it has decimal values inside of it and let's come in here and let's change this up a little bit because they're not as precise as decimals so basically this this is where we're going to end this and let's get rid of the M right there and let's go and shorten this guy down as well and let's go and change this to doubles so that we will be able to see that we're working with doubles on this so dbl and let's change this to dbl and this to dbl as well just so we don't cause confusion and that means this has to be dbl and this has to be dbl once again all the code is available in the description with a transcript and change this to double we're also going to print out the max value for our doubles so I'm going to change this to biggest double and I'm just going to disregard this so I don't have to you can go and do that if you'd like and here to print this out as a string value what we're going to do is we're going to change this to double and max value and then if we want to change this to a string we're going to go to string like that and inside of here put a hash symbol and that's going to allow us to print that out make sure you have that last parenthesis there and let's run that and see what we get and you can see the doubles are quite large as you can see but they're also not precise so we can see right here whenever we add these together nine eight one you can see right here seventy nine and we added two to it that time you can see that that's going to work but if you increase this by just one digit whoops I went and tried to do something I shouldn't do all right let's close this off if you would go and increase this by just one value you're going to see that those values are going to be disregarded all right so the whole point of this is to just introduce you to the biggest and largest numbers you can store in these different data types and also to show you how precise they are and how they can cause you problems if you don't pay attention to that precision now talk about floats and we'll get the biggest float a float is a 32-bit floating-point number and it is not as accurate as a double it's half as accurate actually and we'll go floats at max value but normally you're not working with gigantic values so you normally don't have to worry about that and this is a lowercase float like that and that's going to print out the biggest float that we have we're then going to change this from double to floats and we're going to change this to float and let's go and change this to float once again and this to float and then we're going to have to shrink this down because we can only work with six digits see it's much much smaller so let's get rid of that oops put the two back inside of there then we're going to have five zeros and get rid of this and then we can come in and add these up so this is going to be float and this is going to be float as well got all that saved and we can execute it look have an error that the depth at all the reason why is I forgot to put the F there say it's good I made the error and that's going to point out the fact that you could make that error it's common error and if we run it you can see right here if we move this up that it was able to add two to that value right there however if you tried to add one more decimal place it's going to foul up and you can also see the biggest float you can work with but again it's not precise if you work with that gigantic number and there are also a whole bunch of other different types of data types you can work with and here they are you can also work with bytes and you can see how big those numbers are you can see here you have characters those are going to be one individual character s byte and how big those are and so forth and so on so those are all of the other different data types aside from strings a couple other I'm going to introduce but those are the main number types that you're going to work with it's also important to understand that you can convert from strings to other types with a function called parse so let's say that I create a boolean I'm going to call this boolean from string is equal to so normally you're going to be receiving input and it's going to be received as a string and if you want to change it to to an actual other datatype this is how you do it you go bull parse and then let's say this is true and you can put that in there and that's going to convert that for us and you're also going to be able to go int int from string is equal to int parse and in that situation let's say it's 100 and of course that also means you're going to be able to do this with doubles and with decimals and all these other different data types but I can get a double from string just to give you an example and to do that you would go double and parse and in this situation one point two three four alright so that is how we can convert from strings to other different data types which is also a very useful thing to be able to do so let's up about some other additional data types you might want to work with let's say you want to work with dates and times well if you want to work with a date use a type called date/time and let's say we go awesome dates and we want to define a date inside here so what we're going to do is go equal to new date time and this is a date time object 1974 12 and 21 probably the most important day ever in history and we can go and print that information out onto the screen and we can print out additional information so let's say that we wanted to get what it was the day of the week whenever that date occurred and we can go and we can find out and how we can find out is just by going awesome date like this and day of week and close that off of the semicolon you're also going to be able to come in here and change these values to maybe not so awesome of a date but what the heck we have to come in here and do something so to do so we would go awesome date and we wanted to add say four days to that awesome dates we could do so like this we could also in a very similar fashion come in and add in months and just change this to months and what's today that we want this to just be one and we could also go and add another year to it just by going years like that and let's just add one to that and then we want to come in here and print out that information so we can go and print the entire date out by going console and right line of course and this new date and we'll throw that inside of there and to get the entire date we go awesome date followed by date and run it and you can see the day of the most awesome date in history was on a Saturday and the new date based off all the changes I made or what you can see right there all right it's a cool stuff let's get rid of that we're also going to be able to work with x in a similar way using what are called timespan objects so to create one you go time span and let's say we want to define lunch time equal to new time span and let's say that it is at 12:30 and 0 seconds there we go we just created a time span object we're then going to be able to come in and go lunch time and let's say that we need to change our lunch time we can do so let's say we want to subtract 15 minutes from our lunch time so we can go in there and go subtract and new timespan for actually creating another object to subtract from it there's the 15 minutes and there is the seconds we're also going to be able to add time so it's that we want to we had a problem with our time to get our lunch we can go add and let's say it's an hour later and the minutes are going to be zero and that's that and then if we want to go and print out or into our new lunch time we can go write line and new time and get the actual lunch time and to do so we go to string and to string is a function remember up here we were talking about how to parse values to change them from strings to other numbers well we often use another function called two string to convert other values into strings close that off with a semicolon and if we run it you're going to see the changes of time all right so there we are that is date/time as well as timespan another thing we can work with our what are called big integers and they are used to store very very large numbers but to use them what you're going to have to do is come up here to project and click on that come down here and go and click on add reference under assemblies inside of frameworks you're going to want to come in through here and look for system numerix see that right there and you're going to want to make sure there's a check mark inside of there and then click on OK then scroll back up to the top of all of your code and then come in here and type in using system numerix exactly like that and then you'll be able to make big integers likewise even if you were on a Mac you're going to be able to do the same thing just come up here click on project edit references click on that and just leave all selected and then look for system numerix and that's right there put a check mark inside of there click on OK and the world's awesome alright back into the windows world so what we're going to do here is I'm going to show you some things we can do with big integers now to define a big integer what we're going to have to do is to define the value using what are called text literals and you don't need to memorize all of these different things I'm saying right now just after I do them a whole bunch of times you're going to remember them and to do that you're going to go parse like this and then we can throw a big value inside of here I'm not going to do much with this just wanted to introduce you to it you can go ahead and play with it as much as you like afterwards there we are so we just created one and then we're going to be able to work with them just like any other data types so we'll be able to go right line and go something like big number times 2 is equal to and throw a value inside of there and then we'll be able to come in and say big num times 2 just to demonstrate that they are used just like any other value and if you see it there they are alright so there we go that's a big integer now very often you are going to want to format your output in specific ways and there's a whole bunch of different formatting options that are available to you so we will say console and right line and for one thing you can output information as if it is currency and to do so inside of those brackets you're going to put your normal zero like you have there but you're going to say hey this is currency alright so that's how easy that is come in here and let's say you get to three point four five five which is in currency or normal currency and I'm going to show you a whole bunch of different formatting options here all at one time so we don't have to keep doing that so let's say we want to also pad the values with zeros we can do that as well and how you do that is come in here and put d4 and let's just change this to 23 what else can we do we can have it only show three decimals like that and then change this to f3 and throw another 5 on there it's only going to show 3 and why don't we do one more let's say we want to add commas and decimals we can just change this to comas and then come and get rid of this and say N and 4 and for this guy we're going to say 2300 and if we execute that you're going to see how all those work out for us the 4 with the N for that you see right here that is going to make sure we have 4 spaces in the decimal places it also puts commas inside of that 3 decimals is going to cut off whatever is over here and round it up you're going to see that this guy right here d4 is going to make sure that we get 4 values whether we have known or not and this is currency alright so there's some formatting options that are available to you and now I'd like to take a look at strings now stir things are going to allow us to store a series of characters and you've seen them before to define a string just go string then we'll say random string and let's go and give this a value this is a string just to keep it simple now you're going to be able to come in here and get the number of characters that are inside of the string or the string length as it's often called just by going random string and then following that up with length that's also a way you could cycle through a string and output that information on the screen let's go copy this and let's see some other string functions that are available to us we are also going to be able to come in and see if a value is contained inside of a string so we will say something like string contains the word is inside of it you can also go like this after the comma and move that down there so that you can see things a little bit easier and find out if it does contain something you just go contains and whatever you are searching for and it will then jump back and tell you if it contains it or not let's go and do some more look make sure you put that last parenthesis in there all right let's see some more string functions that are available to us and let's go and put this down here as well so you'll be able to see all those at one time you're also going to be able to find the index of the beginning of the match of a string so we will say something like let's go put a space there index of is and we'll just change contains into index of and that's going to give us that go get some more let's say you want to remove the number of characters starting at an index I'm just going to call this remove string and to do that we can say remove and we want to start at the zero index and we want to remove six characters after that zero index inside of that let's go and do some more let's say you want to insert a string well let's just go and call this insert string and to insert you're just going to say insert like that and we're going to say at the 10th index we want to import the the name of short and put a space inside of there we can also come in and replace a string and it's going to find whatever we are searching for and it's going to change it into whatever we tell it to change it into so we'll just call this replace string and replace it of course we're going to use replace and what we are specifically looking for is the word string and where it finds it it's going to change it into sentence we're also going to be able to compare strings and what this guy's going to do is if the value that is returned from this function is less than 0 that means that string 1 is going to proceed string 2 if they're equal it's going to return a value of 0 and if it returns a value that is greater than 0 that means that string to proceed string 1 and let me show you an example here that will make more sense let's go compare a to B and here what we're going to do is change this into a string this is a string object and string object function or method or whatever you want to call it and what we do here is we pass in the two strings that we want to compare I'm just putting single characters in there but that doesn't mean you have to and if I want to compare these strings and ignore case what I can do is go comparison dot original ignore case well we run these just to see what they look like there you can see string lengths it went and counted all those characters did it contain the word is yes index of is the very first index anyway is to remove string you can see here it remove the very beginning of our string insert a string you can see it puts short inside of there replace string you can see it changed string of the sentence and here it returns a negative value and what does that mean that means that string a or the letter A and here precedes the letter B whenever we compare those two strings alphabetically let's now come in and let's see if two strings are equal to each other so what I'm going to do is go a is equal to lowercase a and this is why you'd like to connect compare values with and ignore case and do this we are going to come in use the string object again and use a function called equals and then inside of it we will pass what we want to compare us will say a to a how that get in their hands get rid of that and then we will follow that up with our string comparison that got right there and then original ignore case once again do a couple more here we can add padding to the left side of the screen so let's say our left side of strings so we'll say something like pad left and what this is going to do is add padding to the string as a whole it's not going to add to like if you put a 5 inside here it's not going to add 5 to it it's going to add whatever is over the total size that you define so what we're going to do to do that is go pad left and let's say that we want it to be 20 total spaces in size and everywhere to pad we want to put periods we can put spaces of course and let's go copy that and we can also do padding on the right side of whatever gets printed out again let's leave this be 20 and that like that and we just change this to pad right go and do a couple more to finish off the strings we're also going to be able to trim whitespace so let's just change this to trim there is no white space but whatever it'll work if there is here random string and just put trim inside of there and nothing and it will get rid of all the white space on the left and right side of our string and we're also going to be able to make this uppercase which is sometimes a good way to compare strings so uppercase and for that you just changed this to upper and guess what you're going to do pretty much exactly the same thing if you want to convert every value in the string to lowercase just change this to low and change this to low as well and also you're going to be able to use the format function and to go and create strings so we can say something like string new string we want to creates is equal to and let's say you want to do something like Mad Libs I don't know if anybody out there even knows of Mad Libs are and but whatever we're just going to be placing values inside here so let's say we have something like this saw a and then we're going to throw in something else inside of there and the next one is two in da and three like this exactly and then after that we could say something like Paul is going to be the first value and then rabbit and then eating and then filled not exactly the most uproarious funny Madlib you ever saw in your life but we have that another thing that's important to understand with strings is let's say you wanted to have a string and it had a double quote inside of it inside of the double quote well if you have the double quote here it's not going to know where these the definition the string begins or ends so if you want to put a double quote ins out of there you put a backslash like that and your double quote likewise you can also do a backslash let's backslash and double quote like that not double double quote and likewise you can do a regular quote inside of there your also if you want to do a backslash inside of your string do double backslashes can also do a tab and you can also do an alert and alert inside of a string whatever it runs is going to go and play a beep noise and you can play around with that on your own and also there are things here to finish off called a verbatim strings and if you go and do something on a console right and then put an @ sign inside of there exactly what I typed and then you can put something like a single quote inside of there and a backslash like that it's not going to matter because it's going to print out exactly what you typed and you can see right there we didn't have to use back slashes to use those guys and there is all the outputs from all the other string functions that I defined in my code and there you go guys that is a great introduction to c-sharp or at least I think it's pretty good don't want to overwhelm you and I'm going to fairly quickly whip through the c-sharp language over the next couple videos but I'm going to provide an immense amount of detail in comparison to my learning 1 videos and then we'll move on to the Sumerian and making games and unity and a whole bunch of other different things so just like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 435,772
Rating: undefined out of 5
Keywords: C#, C# Tutorial, Learn C#, C# Progamming
Id: 0p0JLFZj2C8
Channel Id: undefined
Length: 40min 6sec (2406 seconds)
Published: Sat Jan 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.