C# Tutorial 4 Methods Enum

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 4 of my c-sharp video tutorial in this part of the tutorial we're going to focus completely in on methods or functions or whatever the cool kids are calling them we're going to talk about pass by value versus pass by reference we're going to talk about out params named parameters method overloading enum and a whole bunch more and like always all of the code as well as a transcript of this video is available in the description underneath this video and I have a lot to do so let's get into it okay so here we are in Visual Studio or if you are on a Macintosh you were using as Maron okay the very first thing I'm going to talk about here is well the difference between pass by value versus pass by reference now by default values are going to be passed into a method or function same type thing and not a reference to the variable itself that is the default way of dealing with passing values and two methods and the change of what that means is the changes made to those values inside of the methods are not going to affect variables outside of the method so let's come in here and look at an example because that will make a lot more sense I'm going to create a method here and it is going to return a double and it is going to be called get sum and it is going to be receiving a double which I'm going to call X it's also important to note here that you can put in and where a sign of value to a parameter directly inside of there so that if nothing is passed everything will be okay and I'm just going to put one inside of both of those guys and then what is going to do I'm going to do a little bit of some crazy stuff here I'm going to create a temporary variable and assign its value of whatever X is I am then going to get X and assign the value of Y to it and then I'm going to take Y and assign the value of temp so what I did was I switch the values of x and y passed inside of there now the only reason I'm going to do that is because I want to show that the values of x and y are not going to change even though I tried to change them in the method here and then amen and just go return X plus y all right so now we're going to come up here inside of Maine and use this method so we're going to create a double up inside of here of course and I'm going to give it exactly the same name not that that's going to matter but I'm going to give it that name and I'm also going to go Y is equal to four and then I'm going to come in here and I am going to output the information on the screen that I get from my method so I'm going to go five four is equal to and go and get that value so I'll call get sum and then I am going to pass in x and y like that now to show that the values indeed did not change I am then going to come in here and once again go to console and write the new values for X and y or whatever the current values of x and y to show that by default everything is pass by value and then I can just put X to the side up here and if we run this you're going to see that it went in there and it added those together came back with the value of nine however the value for x is still five and why is that well it's quite simple everything by default is passed by value I'm going to show you the difference in regards to pass by value and pass by reference but first I want to talk about the out parameter now you are going to be able to pass a variable as an output variable even without assigning a value to it so let's say we have something like solution all right so there's no value assigned to that and what is going to make this an out parameter is the fact that we will be able to pass a parameter using out and it is going to actually have its value assigned inside of the method without throwing an error so we're going to say let's say we want to create a method called doublet and we're going to pass in the value that we want to double and then we are going to store this value inside of this variable this int right here called solution and then of course we can come in here and output the new value for this so we will say 15 times 2 is equal to and then throw a solution inside here and as you can see there's errors popping up because it doesn't know what's going on so now what we're going to do is come down here and utilize this out parameter and whenever you're defining your method just make sure that you use out inside of the parameter area so here is double its and like you saw it's go and receive an integer and then it is going to assign a value to solution which is back inside of there so this would be an example of how we can manipulate a variable outside of a method and if we run this guy you're going to see that indeed 15 times 2 is equal to 30 of course but more importantly all of the errors went away so that is one way that we can come in here and actually work with values that are outside of the method itself and also define the value for a variable inside of a method itself but now what I want to do is actually show you pass by reference and how we can change values inside the method and have them affect the local variable that is outside of that method now if you want to come in here and receive a variable by reference create in another method here what I'm going to do is go and create something called swap it's going to receive you can see right there I'm saying that I'm expecting a reference to the actual variable right there and once again reference in num2 and then what I'm going to do is I'm going to do exactly what I did before I'm going to go and create a temp it's going to hold the value of num1 I'm then going to go and get num1 and assign it the value of num - and then num2 is going to receive the value that was originally stored in temp so it's in essence just switching those around ok pretty simple stuff now inside of main I'm going to call this guy so I'm going to go and create num1 and give it a value of 10 just for the heck of it and num2 is equal to let's say 20 and then come in here and call this for it to run and I'm going to say B for swap and I'll get num1 and i'm going to put the value inside of there and then I'll also go num2 and put its value inside of there and num1 and num2 and then I'm going to actually call the function that is going to perform the swap and this will just be swap and here I'm defining I'm passing by reference so I can expect to change the values for those guys and num2 and then after the swap function is called I am going to say I'm just going to go after swap and if we run that you're going to see before swap was called the value for num1 was 10 and afterwards it was 20 and likewise for num2 so you could see that we were able to pass by reference there adjust by going in and saying the reference right here for that variable as well as reference right there so pretty simple stuff and that is an example of pass by reference versus pass by value and now I want to talk about params now you're going to be able to pass a variable a variable a variable meaning numerous different types or different amounts of data of the same data type into a method using params and also you'd be able to pass in an array itself so what I'm going to do here is I want to well let's come down here and actually create the function first because that makes sense so we'll go static and it's going to receive a or return a double and I'm going to call this get some more Animus a put params inside of here you'd actually be able to receive the params array but also receive other parameters but the params parts or params parameter must come last all right so that's just important to know because otherwise it wouldn't know when the list ended and you can go in on your own and do that and see how that works so what we're going to do here is I'm going to create a double called sum and give it a value of 0 then I'm going to use my for each function in I in norms and then I'm going to come in here and just add in each value from the parameters array that was passed inside of here and once again this is exactly the same as if we would go sum is equal to sum plus I all right same exact thing just a shortcut function and after that is all done of course we're going to come in and go and return these sum so what this is going to do is receive a variable number of parameters and add them up and return it okay simple stuff and we will come in here and we will go and pass in a variable number all variables and so we go two and three is equal to and get the return value from that and then go get some more and of course that's in one two and three inside of that guy and if we run it you're going to see that it indeed works one plus two plus three is equal to six last time I checked that was true and indeed we were able to pass in a variable number of values and work with them in our method and moving right along now I'm going to talk about named parameters so let's say that we have a function or main method and it is called print info and it receives a string and it receives a name and it also receives a zip code all right so pretty boring so far now what it does is it comes in here and it prints out all of this information on our screen so we'll say something like blank which is going to be the name lives in the zip code and then go and get the zip code and of course it name and we will get zip code and we will put those inside of there now let's say that up here inside of main you for some reason want to pass in zip code and then name can you do that yes you can if you use name parameters so how that's going to work is we will go print info and we will go zip code like this and actually put in the variable name followed by five one four seven and then put the name inside of here and then inside of there we will put a random name and you're going to see that yes indeed you're able to do that so let's run that as long as you define as the name and the other different variable types inside of there you're able to send them in in any different order okay so just a brief explanation of name parameters and how they work now let's look at something a little bit more interesting which is method overloading alright so again I'm going to use get some here and it's going to receive two doubles and it's in return the sum of those you can pause the screen if you want to type that in and what I'm going to do here is jump up inside of here and actually go and call this so I'm going to go and do right line and we're going to save five plus four is equal to and put in whatever the value is well actually let's go 5.0 and 4.5 like that alright and then we're going to pass in those values in to get some and get a result from it so we'll go 5.0 and 4.5 and if we run this you're going to see it has no problem adding those ideas together so let's come in here and let's change this a little bit let's go and copy this and let's change this to five and change this to four five and four and run it again and you can see indeed no problem there either what however would occur if instead of passing in either doubles or integers we instead passed in a string representation for five and four well as you can see right here is going to say that you cannot convert from string to double well that's a problem the nice thing however is that we will be able to come down here and actually recreate this using the same exact name so we're going to keep this get some but we are going to change this into string and we're going to change this industry and what that's going to allow us to do is then here just use convert again I'm going to go and define a double call it double X is equal to I'm going to call convert two double and throw X inside of there and then likewise I'm going to do exactly the same thing for y changes to Y and change this to Y and we can go and change these default values right here to strings as well and then just change this to double X and then change this to double Y and even though the name of the function is exactly the same because of method overloading we are going to have no problem calling the right method and as you can see we run it you can see five plus four is equal to nine comes back perfectly fun and the only thing to pay attention to here is that if you are going to overload a method that the parameters must change in one way or another so if see here we have double double and here we have string string that's perfectly fine we could have one of these be a double and one of them be a string and it would still be allowed so the parameter values must change in some way and no just changing the return value is not going to allow for method overloading so there's an example of method overloading which is extremely valuable and now finally I'm going to take a look at in ohms because I have a little bit more time now an enum is going to act as a custom data type with key value pairs and they are going to allow you to use symbolic names to represent data rather than just using numbers and things like that they don't that fun so let's say we want to create something that is going to represent a car color which is going to be a custom data type we would like to use and let's say that we want to define this is a byte that is how the index or the key for each piece of this custom data type is going to be represented if you do not put this here it is going to use an int but since we know we're using a very limited number of values we're going to make it a bite just to save some memory then what we can do is we can define all our colors and we could by default this is going to assign a value of zero to this but let's say we want it to be one we are then going to find blue and blue is going to get the index value of two and so forth and so on and we'll throw green inside of here and we will read inside of here and we will throw yellow in all right so those are going to make up our custom car color data type what we are then also going to do is we're going to create a method that is going to work with this enum and I'm going to call it pink car it is going to receive our car color data type I'm going to call it CC and then it is going to write out some information about our cars so it's going to get passed this and then it's going to come in and say the car was painted and it's going to output with the code I'm going to output both the color of the car was painted as well as the code or in this situation the index for that guy so then we can come down here and just get CC and then we can also go and cast this into an integer so that we can see what that is all right so there's that now let's jump up into main and it's very important that the enum is defined in the class not inside of main otherwise you'll get an error now what we'll do is we will define car color and let's just call this car one equal to and how we would assign this value is to go car color blue and then you will see that if we call paint car and pass in car one that it is automatically going to go in there and paint our car using our custom data type and you can see right there the car was painted blue with the code - all right so I'm going to get more into enums later on in some real-world examples but that was a lot of information on methods as well as enum and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 66,496
Rating: undefined out of 5
Keywords: C# Tutorial, C# Methods, C# Method Overloading, C# Out Parameter, C# Params
Id: ERJeRkUb51I
Channel Id: undefined
Length: 17min 14sec (1034 seconds)
Published: Wed Jan 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.