Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
It's important to know that having a method print output using Console.WriteLine is not the same as having a method return a value.
It's important to know that having a method print output using Console.WriteLine
is not the same as having a method return a value. If you confuse the two, your programs are going to behave strangely, and you won't know why. So I wanted to spend this video clarifying the difference.
- Here's a version of our program with the
Subtract
method removed. - Suppose I remove the
return
statement from theAdd
method, and instead passed the result of adding the numbers toConsole.Writeline
:Console.WriteLine(first + second);
- I'll get an error: "'Program.Add(double, double)': not all code paths return a value"
static double Add(double first, double second)
{
Console.WriteLine(first + second);
}
- Converting the return type of
Add
tovoid
gets theAdd
method declaration to compile...- But then we get the error "Cannot implicitly convert type 'void' to 'double'" when the code down in
Main
expects a return value.
- But then we get the error "Cannot implicitly convert type 'void' to 'double'" when the code down in
static void Add(double first, double second)
{
Console.WriteLine(first + second);
}
static void Main(string[] args)
{
double total = Add(3, 5);
Console.WriteLine(total);
}
- Just remember, if you want your method to give information to a human, you can call
Console.WriteLine
. - But if you want your method to give information back to your program, you need to specify a return type and use the
return
keyword instead.
static double Add(double first, double second)
{
return first + second;
}
It's important to know that having
a method print output using console dot
0:00
WightLine is not the same as
having a method return a value.
0:04
If you confuse the two, your programs
are going to behave strangely, and
0:08
you won't know why.
0:11
So I wanted to spend this video
clarifying the difference.
0:12
Here's a version of our earlier program
with the subtract method removed.
0:16
Suppose I remove the return
statement from our add method, and
0:20
instead pass the result of adding
the numbers to console dot WriteLine?
0:23
If I save this and attempt to run it,
I'll get an error.
0:28
This is on line five, which is right here.
0:32
And it says not all code
paths return a value.
0:36
I can get the add method to compile
by changing its return type to void,
0:39
since it doesn't actually
return a value now.
0:44
But if I save this and
try running that, dot net run,
0:48
I get an error down on line 12.
0:52
It says cannot implicitly
convert type void to double.
0:54
The code down here in Main
expects a return value so
1:01
that it can assign it to this variable.
1:04
But the add method is no
longer providing one.
1:08
So just remember, if you want your
method to give information to a human,
1:11
you can call Console dot WriteLine.
1:14
But if you want your method to give
information back to your program,
1:17
you need to specify a return type.
1:20
So let’s change this return
type back to double from void.
1:22
And you need to use the return
keyword return first plus second.
1:30
Let me save this and try running it.
1:35
And now, it's working again.
1:40
You need to sign up for Treehouse in order to download course files.
Sign up