Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let's learn how to call methods.
Before we could even start on the cat food store program, we had to spend the first stage learning about variables, types, and namespaces. But now we're ready to actually begin work on the store.
Cat Food Store Features
- Display welcome message
- Ask for quantity
- Calculate total
- Discount for large orders
The first requirement is to display a welcome message to the user. Let's work on that now.
- If you click the "Launch Workspace" button on this video's page, it will start a workspace with a new C# project.
- There are a few files here, but there's really only on we care about: the
Program.cs
file. - C# source code is stored in plain text files, but instead of
.txt
, the file name usually ends in.cs
. - If we click the file to open it, we'll see that it's set up and ready for us to write code.
Program.cs
:
class Program
{
static void Main(string[] args)
{
}
}
- We need to add our code between the two curly braces following this
static void Main
line here. We'll be talking about what thisstatic void Main
line means in just a moment. - We need to print a welcome message for the user.
- But as we showed in the previous stage, we don't want to have to type the
System
namespace all over our code.- So here at the top of the file, let's use a
using
directive for theSystem
namespace:using System;
- And then we can delete the
System
namespace beforeConsole.WriteLine
.
- So here at the top of the file, let's use a
- We can try running this by going to the console area and typing:
dotnet run
- The .NET framework will find our
Program.cs
file, compile it, and execute the code it contains. - And there in the terminal we'll see the string we specified.
- The .NET framework will find our
So we were able to print a welcome message by calling the Console.WriteLine
method. We've talked about methods briefly in the first stage, but we never really explained what they are. It's time to do that now.
- When we called
GetType()
on various values to see what their type was, that was a method:42.GetType();
- When we called
ToUpper()
on a string to capitalize it, that was a method:"hi".ToUpper();
- And we are actually writing code for a new method right now. This
static void Main
code here defines a new method, a special one that gets called when our program starts. - A method is a group of code statements that are put together to perform a particular task.
-
WriteLine
is a method. The task it carries out is to write a line of text out to the console.- You run a method by calling it.
- To call a method, type its name, followed by a pair of parentheses:
Console.WriteLine()
- When calling a method, you can provide arguments inside the parentheses. Arguments are values associated with a method call that change what the method does when it runs.
- Let's write the string
"Hello"
here within the parentheses, so it's passed as an argument to the method:"Hello"
- Most methods have a specific number and type of arguments they expect.
- Some methods expect no arguments. Others expect two, three, or more.
- Let's write the string
- And of course, every statement in C# needs to end with a semicolon, so let's add one here at the end of the line:
;
- Let's try a different method call:
System.Threading.Thread.Sleep();
- This method causes the program to wait for a certain amount of time before continuing.
- How does the
Sleep
method know how long it should wait? We can pass the duration in as an argument to the method call. - It takes an integer with the number of milliseconds (or thousandths of a second) it should wait for.
- So
1000
milliseconds would cause it to sleep for 1 second. - We type the argument here between the parentheses:
1000
.
- Let's also add another call to
WriteLine
, so we can tell when the program is done sleeping:Console.WriteLine("done sleeping");
dotnet run
- Let's try changing the argument to
Sleep
so that it pauses longer. I'll change it to 3000 milliseconds so it sleeps for 3 seconds:3000
dotnet run
- A quick issue with the
Sleep
method that I want to address before we move on.- You'll notice that
System.Threading.Threads.Sleep
appears to start with theSystem
namespace. - Since we have a
using System;
directive at the top of the file, you might think we can removeSystem.
from the front of the method name. - But let's try to run this:
dotnet run
- We get an error: "The name 'Threading' does not exist in the current context".
- The problem is that
System.Threading
is a separate namespace fromSystem
. So we can't just removeSystem
from the front of the method name. - Let me undo that change.
- Instead, we need to also add a
using System.Threading;
directive at the top of the file:using System.Threading;
- Note that we're not replacing the
using System;
directive, we're writing this directive in addition. - Now that we have a
using System.Threading;
directive, we can remove notSystem
, butSystem.Threading
from the front of the method name. dotnet run
- You'll notice that
So now you know - whenever we've been typing a name followed by a pair of parentheses, we've been calling methods. Up next, we'll show you how to define your own methods.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up