Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed C# Basics!
You have completed C# Basics!
Preview
We can get our methods to accept arguments using method parameters.
- It would be nice to be able to specify how long our
Waitmethod should pause for. - We could do that by passing it an argument. For example, we could pass it the number of milliseconds to wait for:
Wait(3000); - But if we try to run that code, we get an error: "No overload for method 'Wait' takes 1 arguments".
- In other words, a call to
Waitcan't accept an argument... yet. - We can get it to accept one using method parameters.
- Here's a program that declares an
Addmethod that accepts two parameters, and aSubtractmethod that also accepts two parameters. - A parameter is a special variable that you declare at the start of a method.
- It's just like declaring a variable; you specify a type and a name for the parameter.
- You can specify multiple parameters by separating them with commas.
- Within the method body, you can access the parameter just like you would a variable.
- In the
Addmethod, we print the value of thefirstparameter, and then print the value of thesecondparameter. - Then we print the result of adding
firstandsecondtogether.
- In the
- When a method takes parameters, you need to provide argument values when calling that method.
- C# sets each parameter variable with the value in the argument.
using System;
class Program
{
static void Add(double first, double second)
{
Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(first + second);
}
static void Subtract(double first, double second)
{
Console.WriteLine(first);
Console.WriteLine(second);
Console.WriteLine(first - second);
}
static void Main(string[] args)
{
Add(3, 5); // => 8
Add(10.5, 7.2); // => 17.7
Subtract(9, 3); // => 6
Subtract(21.3, 7.1); // => 14.2
}
}
- We can update
Waitwith a parameter, and pass arguments when we call it:
using System;
using System.Threading;
class Program
{
static void Wait(int delay)
{
Console.WriteLine("Waiting...");
Thread.Sleep(delay);
Console.WriteLine("Done");
}
static void Main(string[] args)
{
Wait(1000);
Wait(3000);
}
}
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
We could do that by
passing in an argument.
0:00
For example, we can pass at the number
of milliseconds to wait for.
0:00
3,000 milliseconds will cause
it to wait for 3 seconds.
0:05
But if we save this code and
try to run it,
0:09
We get a compiler error, no overload for
method Wait takes 1 arguments.
0:15
In other words,
a call to wait can accept an argument.
0:20
We can give it to accept one
using method parameters.
0:23
Here's a program that declares an Add
method that accepts two parameters and
0:27
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