I don't understand the point of using out in this C# lesson

This is a little bit long, but I appreciate any help anyone can provide. This is C# Part 4 - Method Output lesson 5. Using Out.

This is the answer that Codecademy gives if you ask for solution. But you can get the same result without the out bool marker, so I don’t understand the purpose of using out here.

      string statement = "GARRRR";
			string murmur = Whisper(statement, out bool marker);
      Console.WriteLine(murmur);

I also don’t understand where marker comes from. It’s not declared anywhere I can see it. How come it doesn’t read Whisper(statement out bool wasWhisperCalled); like it does when you’re creating the method?
Here is the full solution provided by Codecademy:

using System;

namespace UsingOut
{
  class Program
  {
    static void Main(string[] args)
    {
      string statement = "GARRRR";
			string murmur = Whisper(statement, out bool marker);
      Console.WriteLine(murmur);
    }  
    
    static string Whisper(string phrase, out bool wasWhisperCalled)
    {
      wasWhisperCalled = true;
      return phrase.ToLower();
    }
	}
}

And here is my solution:

using System;

namespace UsingOut
{
  class Program
  {
    static void Main(string[] args)
    {
      Whisper("Hello", out bool param2);
    }
    
    static string Whisper(string param, out bool param2)
    {
      //declare string sentence variable
      string sentence = param;
      //set sentence to lower case
      sentence = sentence.ToLower();
      //set bool to true, not sure why but codecademy liked it when I did this.
      param2 = true;
      //print to console
      Console.WriteLine(sentence);
    }
	}
}

It got a few errors, but it passed all the codecademy tasks. It doesn’t print and not all code paths return a value. I figure I could move Console.WriteLine to Main() to fix the printing. But I’m pretty lost with the out bool marker part.

Hi @giantgough - welcome back to the forums! :slight_smile:

I’ll do my best to provide some clarity, though I’m going to answer parts of your post out-of-order because I think it’ll help clear things up.

Since C# 7, we’ve been able to declare the out variable inside the method call. So, we can either do this:

bool marker;
string murmur = Whisper(statement, out marker);

or this:

string murmur = Whisper(statement, out bool marker);

The second version is more compact, but because we’re declaring the variable inside the method call we need to provide the type - bool in this case. (For C# 6 and older versions, the only acceptable way to do this was the first example where we declare the variable ahead of the method call.)

In the method definition, wasWhisperCalled is the parameter. In the method call, we’re declaring a new variable called marker to hold the value which comes back from the method through the out parameter. marker is just a variable in the method call, so we can name it whatever we want.

I’ll have to reset the exercise and try your code to be sure, but I think you’ve managed to fudge it because the SCT is simply checking for the presence of some expected lines of code and not actually validating the output… As a result, your code isn’t a valid solution to the final task - it doesn’t do what is being asked, as you mention…

You couldn’t move the line Console.WriteLine(sentence) to the main method verbatim without changing the variable being printed, but even then your code would still fail and not write anything to the console. This is because you’re getting the error message Not all code paths return a value; your method declaration specifies that Whisper() returns a string but your method never does so you get an error rather than hello written to the console. You need to add a return statement to the method. :slight_smile:

Which brings us back around to…

The lesson asks you to create a method, Whisper(), has two parameters: a string and an out bool.

As I mentioned earlier, when we call the method we can pass in or declare any variable we like for the out bool parameter - Codecademy’s solution has opted to use a variable marker, whereas you went for the same name as your method definition and used param2.

However, the two solutions do not generate the same result. The Codecademy solution ends up with the variable murmur assigned the value garrrr and marker assigned the value true, with both being in-scope and printable to the console from within main(). Your solution fails to build because of the not all code paths return a value error.

If we focus on the out part

The exercise is, I think, simply meant to get you used to using out as a fudge to work around a limitation of C# when you want to return more than one thing from your methods. As it happens, I don’t think the exercise requires or checks whether you print the out bool variable so it is sort of pointless here - aside from getting you to practice using it, that is.

(As an aside, this is a limitation which sort of no longer exists because Microsoft have expanded on their implementation of tuples as of C# 7 and you can return multiple values as a tuple from methods now.)

One final thing. Your solution is easily 95% of the way to being correct; the only issues are the missing return in Whisper() and the misplaced Console.WriteLine(), though the latter is really only wrong because the lesson asks you to print the returned value… so please don’t take this as a huge dig at your code because it’s really not far off! :slight_smile:

Hopefully I’ve not confused you further, and you understand why CC went for out bool marker in their example solution. Let us know if you’ve any further questions, though. :slight_smile:

1 Like

This was extremely helpful. Thank you very much.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.