Methods - C# Mastery Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now that you know the basics of c-sharp and how to create some valuables it's time to work our way towards creating a real application the fastest way to accelerate your learning is to create real-world software it is important to get the foundations of the language right and then move into real-world applications without the Foundation's first you will struggle as soon as applications come to be created that's why we're first covering these real basics of c-sharp and slowly work in towards small applications and then eventually real-world applications we will shortly create an application that asks the user for their date of birth calculates the day of the week they were born on and displays that to the user for now let's continue learning towards that goal in this lesson we will learn about methods a method contains a code block so a bunch of statements inside curly braces in c-sharp every statement is executed inside of a method as you have seen so far the method main is the entry point of the application this is where our statements are executed inside of the main method methods can be defined inside of classes structures and interfaces but we will cover those in future lessons let's jump in to create our own method inside a console application let's create a new dotnet core console application I will give it the name methods looking at this new project you can see the first statement console dot write line is doing what is known as calling a method this means the statement console dot write line calls the method named write line inside of the class console all the code inside of this write line method is then executed line by line until it returns and then the next line below this statement will be executed to see this easier and to understand it better let's create our own method methods are placed inside of the codeblock we can place our method above or below the main method let's go below methods can have their access the ability of other parts of code to discover it and call them controlled by something called an access modifier the access modifier is optional so if you do not specify an access modifier it defaults to private which means only code inside of the same code block where the method resides can find and call this method there are exceptions as always in code to use reflection to execute private methods but the general rule of thumb is private methods are only discoverable and executable in code by methods in the same scope ie inside the same code block don't worry about access modifiers for now we'll cover them in a future lesson just note that my preference is always to explicitly state the access modifier possible values include public where the method is fully discoverable everywhere private where only it's containing class can access it protected internal and a few others we will cover in future lessons for now I will declare my method public using the keyword public notice the main method does not state public private or any other access modifier except static which means by default the main method is private it's equivalent to looking like this if we wanted to turn it public we could have the access modifier public now note the static keyword on main I will cover this in future lessons also so don't worry about why this is here for now but in order for the main method to easily call our method we are going to include the static keyword for now now that we have a public static method meaning our method can be found anywhere in code next we need to specify what type of value our method when you call a method like console.writeline it can return nothing such as in the case of console.writeline the keyword void means nothing is returned or it can return a value if you returned a value it can be assigned to a variable if you return nothing it will simply execute the code inside the method and then return nothing as this method wants to get the date of birth from the user we should return something again until we have discovered classes let's keep it to attack you already know let's return a string then comes the name of our method this is the name you will call like right line let's call our method yet user date of birth always start your method names with a capital and use capitals for the start of each new word this is called Pascal case now when we call a method the code inside the method may want some information to use as part of its code that can be varied for each call in the example of right line the varied piece of information is the string to write to the screen these bits of information being passed into a method are called parameters parameters are like variables we can specify we would like an int variable or a string or any other type we give the parameters variable names so that inside the method code block we have access to those just like we have access to variables for this lesson just to get familiar to how a method is created let's accept a single parameter of type string and call it full name parameters are specified after the name and inside parentheses [Applause] for multiple parameters simply separate them by a comma when we name parameters start them with a lowercase and then every word after start with an uppercase this is called camelcase now to declare the code for the method we open up a code block with curly braces and to complete our method for now let us just return a new string that says the person's name that has been passed in and then make up a day of the week to do this we use the keyword return and now we must return the type we are expected to return which is a string we could just return a blank string like this and the method would be fine for this example we are going to return the full name and then we are going to use the plus to add a string after this string we're going to put a space and we are going to say was born on Tuesday the variable full name used here refers to the full name passed in as the parameter and we can use an access this parameter just like a variable as shown here the return keyword sends the result of this expression back to the caller where it can consume and use that variable as it sees fit you will see that in action in a moment so to compare our method with the main method we have a public static method which means it's got a public access modifier in comparison the main has the default access modifier of private and then it's also static so we'd have a private static method for main and we have a public static method for our method main returns nothing using the keyword void which is the keyword for when a method returns nothing in comparison our method returns String the method Maine has named Maine our method is named get user date of birth Maine accepts an array of strings with the variable name augs our method accepts a string with the variable name of full-name now back to our main method let's call this new method because Maine is inside the same code block and the same scope as this method we can access the method by directly specifying its name only to call a method as seen above here we open up the parentheses and then pass in any parameters our task needs in this case it is asking for a full name as you can see here the full name we specified I will pass in my pugs name as always and a statement with a semicolon now we have called the method the method will return a string and we are not doing anything with the returned value at the minute let's fix that by assigning the returned value to a variable just as we've done in the previous lessons we declare a variable of type string called result and we assign the result the value that comes from the get user date of birth method call so this return value will come back and be assigned to the result a quicker way to writing console.writeline is to simply type CW and then press tab twice now we pass in the result and again instead of typing the full word result as you notice as we are typing Visual Studio recommends the closest match to what it thinks we want to write and highlights it in blue all we have to do in this case is plus R and then tab to complete the word get used to using tab completion in odhh it makes typing code and writing code much faster let's run this code with f5 and as you can see it comes out rusty Malpass was born on Tuesday because the only place we are using this variable result is in this single statement of right line we can reduce these two lines to a single line by taking the result of our method and directly passing it in to the right line method this does the exact same thing but now when we go to right line the result of get user date of birth go straight into the right line parameter don't worry about the name get user date of birth at the minute and it not returning the date of birth as we create the application this method will actually ask the user for the date of birth for now I'm simply using this as an example to show you how methods work if we run that code again to make sure the results are the same you can see we get the same result let's copy and paste this line by simply having the cursor on the line without any text selected pressing ctrl C and control V and changing this to another name press f5 to run the code again and we can see we now have two outputs with different names but the same day of the week so here you can see how we create methods to perform units of work in a nice self-contained code block in this example it is trivial but in real methods as we build up to an application the reuse of code and passing in the variable pieces of information or how we construct larger applications we use methods to break up our code into smaller chunks where typically methods perform small and single actions and other methods then call those methods to make up a code structure ultimately forming our application this is just the very beginning of learning about methods but it is an important step than very early stages of making an application the goal we are now working towards is to be able to ask the user for their name then state their date of birth followed by stating the day of the week they were born to reiterate the method we just created has an access modifier of public static it has a return type of string and the name of get user date of birth it accepts a single parameter of type string with the name full name inside the method code block we return the variable that was passed in plus was born on Tuesday as a string this makes up our first complete method inside of the class program
Info
Channel: AngelSix
Views: 9,427
Rating: undefined out of 5
Keywords: c#, software, development, wpf, vue, vue js, animation, web design, wevb, .net core, asp.net core, free, open source, git, tutorial, beginners, real world, javascript, html, css
Id: oMNDDxAJU90
Channel Id: undefined
Length: 14min 13sec (853 seconds)
Published: Sat Mar 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.